Django __in小写 [英] Django __in lowercase
问题描述
我正在使用 django-taggit ,它处理标签附加到任意内容类型。
我导入了一个大的标签列表,其中包含许多大写字母以及小写字母。
现在,我试图获取另一个类的对象一组标签,但是我想比较不区分大小写。当我这样做:
Media.objects.filter(tags__name__in = ['tag1','tag2'])
包含eg的对象没有找到标签Tag1,只有那些tag1或tag2的标签。
django orm中是否有可能执行以下操作: / p>
Media.objects.filter(tags__name__iin = ['tag1','tag2'])
$ c $ 解决方案
没有简单的方法可以做到。我不是100%肯定的,你可以尝试这样的问题。 从django.models导入Q
q = Q()
标签中的标签.split():
q | = Q(tags__name__iexact = tag)
Media.objects.filter(q )
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"?
解决方案 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)
这篇关于Django __in小写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!