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!
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.
TaggedItem.Meta
defines an index and a unique restriction. It should be included in LowerCaseTaggedItem
. –
Snuff Old question but now there is the following setting to deal with case insensitive tags:
TAGGIT_CASE_INSENSITIVE = True
If you want django-taggit to be CASE-INSENSITIVE when looking up existing tags, you’ll have to set the TAGGIT_CASE_INSENSITIVE setting to True (False by default):
TAGGIT_CASE_INSENSITIVE = True
Source: https://django-taggit.readthedocs.io/en/latest/getting_started.html
/settings/base.py
) –
Balf settings.py
or models.py
? –
Deluna Another option is to monkey-patch Tag.save
method. This way you only add the needed funcionality without duplicating django-taggit code.
from taggit.models import Tag
tag_save_original = Tag.save
def tag_save_pathed(self, *args, **kwargs):
self.name = self.name.lower()
return tag_save_original(self, *args, **kwargs)
Tag.save = tag_save_pathed
© 2022 - 2024 — McMap. All rights reserved.