make case-insensitive tags with django-taggit
Asked Answered
S

4

7

I added tags = TaggableManager(blank=True) to my models, but I want my tags to be case-insensitive. I saw some snippets of work arounds for this and wanted to ask if there is an easy way to do it? If I have to override some methods from the TaggableManager, please advise how can I do that?

Thanks in advance, Arshavski Alexander.

Slipshod answered 14/3, 2011 at 14:48 Comment(0)
C
-2

I have used this snippet for a similar issue. A copy of the code (reprinted for posterity):

from django.db.models import Manager
from django.db.models.query import QuerySet

class CaseInsensitiveQuerySet(QuerySet):
    def _filter_or_exclude(self, mapper, *args, **kwargs):
        # 'name' is a field in your Model whose lookups you want case-insensitive by default
        if 'name' in kwargs:
            kwargs['name__iexact'] = kwargs['name']
            del kwargs['name']
        return super(CaseInsensitiveQuerySet, self)._filter_or_exclude(mapper, *args, **kwargs)

# custom manager that overrides the initial query set
class TagManager(Manager):
    def get_query_set(self):
        return CaseInsensitiveQuerySet(self.model)

# and the model itself
class Tag(models.Model):
    name = models.CharField(maxlength=50, unique=True, db_index=True)

    objects = TagManager()

    def __str__(self):
        return self.name
# now...
>>> tag = Tag(name='test')
>>> tag.save()
>>> Tag.objects.get(name='TEST')
<Tag: test>

Basically, you override the initial queryset so that all querysets take case insensitivity into account.

Clergyman answered 20/6, 2011 at 9:30 Comment(2)
Please don't post the exact same answer multiple times. If the question is a duplicate, flag it as such.Marleen
This doesn't really answer the question. The question involves django-taggit. Your suggestion is basically "write your own tagging app".Lise
S
3

I am sure you have figured out solution by now :) Though I will put the answer as someone might hit this while searching -

Use https://github.com/shacker/django-taggit version of django-taggit.

It allows to configure TAGGIT_FORCE_LOWERCASE = True

It also allows you to filter stop words.

Silverts answered 26/11, 2012 at 22:1 Comment(2)
Does this go into settings.py or models.py?Alteration
Rusl, it would go in your settings.pyHenigman
L
1

Recent versions of django-taggit support a TAGGIT_CASE_INSENSITIVE setting, that should behave the way you're describing.

Lyndes answered 4/2, 2016 at 21:38 Comment(2)
Does this go into settings.py or models.py?Alteration
@rusl settings.pyLyndes
V
0

According to _TaggableManager on 211 line

case_insensitive = getattr(settings, "TAGGIT_CASE_INSENSITIVE", False)

we need to add this parameter in settings.py

Vincenzovincible answered 7/11, 2020 at 21:8 Comment(0)
C
-2

I have used this snippet for a similar issue. A copy of the code (reprinted for posterity):

from django.db.models import Manager
from django.db.models.query import QuerySet

class CaseInsensitiveQuerySet(QuerySet):
    def _filter_or_exclude(self, mapper, *args, **kwargs):
        # 'name' is a field in your Model whose lookups you want case-insensitive by default
        if 'name' in kwargs:
            kwargs['name__iexact'] = kwargs['name']
            del kwargs['name']
        return super(CaseInsensitiveQuerySet, self)._filter_or_exclude(mapper, *args, **kwargs)

# custom manager that overrides the initial query set
class TagManager(Manager):
    def get_query_set(self):
        return CaseInsensitiveQuerySet(self.model)

# and the model itself
class Tag(models.Model):
    name = models.CharField(maxlength=50, unique=True, db_index=True)

    objects = TagManager()

    def __str__(self):
        return self.name
# now...
>>> tag = Tag(name='test')
>>> tag.save()
>>> Tag.objects.get(name='TEST')
<Tag: test>

Basically, you override the initial queryset so that all querysets take case insensitivity into account.

Clergyman answered 20/6, 2011 at 9:30 Comment(2)
Please don't post the exact same answer multiple times. If the question is a duplicate, flag it as such.Marleen
This doesn't really answer the question. The question involves django-taggit. Your suggestion is basically "write your own tagging app".Lise

© 2022 - 2024 — McMap. All rights reserved.