3

I'm using django-taggit, which handles the attachment of tags to arbitrary content types. I imported a large tag list, which contains many uppercase words, as well as lowercase words.

Now, I' trying to get objects of another class containing a set of tags, but I want to compare case insensitively. When I do this:

Media.objects.filter(tags__name__in=['tag1', 'tag2'])

objects containing e.g. the tag "Tag1" are not found, only those ones with "tag1" or "tag2".

Is there any possibility in the django orm to do something like:

Media.objects.filter(tags__name__iin=['tag1', 'tag2'])

that acts like "icontains"?

2
  • Sounds like your better bet is to spin through your tags and convert them all to lowercase. Commented Oct 13, 2010 at 12:44
  • well - yes, that would be possible, but does not solve the general problem. Commented Oct 13, 2010 at 12:47

1 Answer 1

3

There is no easy way to do it. I'm not 100% sure, You can try something like this for your problem.

from django.models import Q

q = Q()
for tag in tags.split():
    q |= Q(tags__name__iexact=tag)

Media.objects.filter(q)
Sign up to request clarification or add additional context in comments.

2 Comments

Since you are using iexact match, you do not neet to use <string>.lower() docs.djangoproject.com/en/1.2/ref/models/querysets/#iexact
This works, thank you. However, I would appreciate an __iin-operator in the Django ORM.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.