Django __小写 [英] Django __in lowercase
问题描述
我正在使用 django-taggit,它处理将标签附加到任意内容类型.我导入了一个大标签列表,其中包含许多大写单词和小写单词.
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'])
对象包含例如没有找到标签Tag1",只有那些带有tag1"或tag2"的标签.
objects containing e.g. the tag "Tag1" are not found, only those ones with "tag1" or "tag2".
django orm 中是否有可能执行以下操作:
Is there any possibility in the django orm to do something like:
Media.objects.filter(tags__name__iin=['tag1', 'tag2'])
这就像图标"?
推荐答案
没有简单的方法可以做到.我不是 100% 确定,您可以尝试这样的方法来解决您的问题.
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 __小写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!