How can I limit django-taggit to accept only lowercase words?

六月ゝ 毕业季﹏ 提交于 2019-12-04 09:39:05

You might want to check out this branch. https://github.com/shacker/django-taggit it has a FORCE_LOWERCASE setting.

It's pretty easy to do with django-taggit. Subclass TagBase and enforce the lowercase constraint in the save method. The rest is boiler point so TaggableManager can use your subclass.

class LowerCaseTag(TagBase):
    def save(self, *args, **kwargs):
        self.name = self.name.lower()
        super(LowerCaseTag, self).save(*args, **kwargs)

class LowerCaseTaggedItem(GenericTaggedItemBase):
    tag = models.ForeignKey(LowerCaseTag, related_name="tagged_items")

class YourModel(models.Model):
    tags = TaggableManager(through=LowerCaseTaggedItem)

You can also enforce a range limit for tag numbers in the save method.

Old question but now there is the following setting to deal with case insensitive tags:

TAGGIT_CASE_INSENSITIVE = True
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!