Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
475 views
in Technique[技术] by (71.8m points)

tags - Django __in lowercase

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"?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...