Django __in lowercase

北慕城南 提交于 2020-01-10 04:26:05

问题


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


回答1:


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)


来源:https://stackoverflow.com/questions/3923589/django-in-lowercase

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!