Django __小写

发布于 2024-09-27 17:53:17 字数 460 浏览 0 评论 0原文

我正在使用 django-taggit,它处理将标签附加到任意内容类型的操作。 我导入了一个很大的标签列表,其中包含许多大写单词以及小写单词。

现在,我尝试获取包含一组标签的另一个类的对象,但我想不区分大小写进行比较。当我这样做时:

Media.objects.filter(tags__name__in=['tag1', 'tag2'])

找不到包含例如标签“Tag1”的对象,只有那些带有“tag1”或“tag2”的对象。

django orm 是否有可能做类似的事情:

Media.objects.filter(tags__name__iin=['tag1', 'tag2'])

就像“图标”一样?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

踏月而来 2024-10-04 17:53:17

没有简单的方法可以做到这一点。我不是 100% 确定,你可以尝试这样的方法来解决你的问题。

from django.models import Q

q = Q()
for tag in tags.split():
    q |= Q(tags__name__iexact=tag)

Media.objects.filter(q)

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文