How can I limit django-taggit to accept only lower

2019-04-10 08:25发布

I'm using django-taggit. I'd like to have all tags in lowercase, also set a range for tag numbers (say between 1 and 5, just like stackoverflow). Is there any way to do it easily with django-taggit? Thanks!

3条回答
甜甜的少女心
2楼-- · 2019-04-10 08:46

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

TAGGIT_CASE_INSENSITIVE = True
查看更多
成全新的幸福
3楼-- · 2019-04-10 08:51

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.

查看更多
\"骚年 ilove
4楼-- · 2019-04-10 08:54

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

查看更多
登录 后发表回答