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

4

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!

Giliana answered 26/5, 2011 at 20:14 Comment(0)
W
3

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

Willywillynilly answered 26/5, 2011 at 20:28 Comment(2)
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 github.com/shacker/django-taggit.git give me 0.9.4 version.Selective
The link gives 404 error now.Snuff
C
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.

Choirboy answered 20/8, 2011 at 8:16 Comment(2)
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.Lewandowski
TaggedItem.Meta defines an index and a unique restriction. It should be included in LowerCaseTaggedItem.Snuff
B
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

Balf answered 8/1, 2016 at 21:28 Comment(5)
Where should I put that line?Dreary
@Dreary In your Django settings file (e.g. /settings/base.py)Balf
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.Dreary
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?Deluna
@RusI Have a look at: docs.djangoproject.com/en/3.0/topics/settingsBalf
S
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
Snuff answered 5/4, 2023 at 16:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.