5

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!

devfeng
  • 281
  • 4
  • 11

4 Answers4

3

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

marcanuy
  • 23,118
  • 9
  • 64
  • 113
  • Where should I put that line? – SMahdiS Jan 17 '17 at 18:17
  • @SMahdiS In your Django settings file (e.g. `/settings/base.py`) – marcanuy Jan 17 '17 at 20:23
  • I really didn't find that file. Where should I seek for it? I am using python 2.7.3 with Pycharm 2016. Don't you mean settings.py in Django project? Please give me a link for more info. – SMahdiS Jan 18 '17 at 05:32
  • There is no such a folder or file in a Django project unless you specifically created it. But in a vanilla Django project should I put it into `settings.py` or `models.py`? – ruslaniv Aug 08 '20 at 16:17
  • @RusI Have a look at: https://docs.djangoproject.com/en/3.0/topics/settings/ – marcanuy Aug 08 '20 at 21:37
3

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

Hutch
  • 729
  • 4
  • 14
  • It work only in django-taggit==0.9.4 so if you install it, it works well. First time i install it by easy_install and get (VERSION = (0, 10, 0, 'alpha', 1)) and it does not have TAGGIT_FORCE_LOWERCASE = True TAGGIT_STOPWORDS = [u'a', u'an', u'and', u'be', u'from', u'of'] options. git clone https://github.com/shacker/django-taggit.git give me 0.9.4 version. – derevo Aug 29 '13 at 15:54
  • The link gives 404 error now. – flomaster Apr 05 '23 at 16:23
3

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.

Michael Mata
  • 126
  • 1
  • 8
  • If you use this method, you might also want to unregister Taggit's own Tag from django-admin, and register your LowerCaseTag model in django-admin. You also need to use the custom TAGGIT_AUTOSUGGEST_MODELS setting if you are using django-taggit-autosuggest. This was not immediately obvious to me. – Pranab Apr 08 '19 at 06:13
  • `TaggedItem.Meta` defines an index and a unique restriction. It should be included in `LowerCaseTaggedItem`. – flomaster Apr 05 '23 at 16:15
0

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
flomaster
  • 1,563
  • 16
  • 16