开发者

Django __in lowercase

开发者 https://www.devze.com 2023-01-20 06:27 出处:网络
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.

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 insensit开发者_JAVA技巧ively. 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"?


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)
0

精彩评论

暂无评论...
验证码 换一张
取 消