如何链接Django的“ in”和“ iexact” queryset字段查找? [英] How can I chain Django's "in" and "iexact" queryset field lookups?

查看:124
本文介绍了如何链接Django的“ in”和“ iexact” queryset字段查找?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名称列表,例如:

I have a list of names, e.g.:

name_list = ['Alpha', 'bEtA', 'omegA']

当前,我有以下查询集:

Currently I have the following queryset:

MyModel.objects.filter(name__in=name_list)

我希望能够以不区分大小写的方式过滤名称。我的第一个想法是使用 iexact 字段查找,但是它似乎不适用于 in 。如何将 iexact in 字段查找一起用于查询集?还是有执行此查询的替代方法?

I would like to be able to filter the names in a case-insensitive fashion. My first thought was to use the iexact field lookup but it doesn't seem to work with in. How can I use the iexact with the in field lookup for my queryset? Or is there an alternate way to perform this query?

推荐答案

这是我的解决方案,它使用Q个对象代替:

Here's my solution, which uses Q objects instead:

name_list = ['Alpha', 'bEtA', 'omegA']
q_list = map(lambda n: Q(name__iexact=n), name_list)
q_list = reduce(lambda a, b: a | b, q_list)
MyModel.objects.filter(q_list)

这篇关于如何链接Django的“ in”和“ iexact” queryset字段查找?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆