Django动态OR查询 [英] Django dynamic OR queries

查看:190
本文介绍了Django动态OR查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 MultipleChoiceField 。我想将我的汽车数据库过滤到已检查的产品,但这会导致问题。如何动态地获取所有的 Q(make = ...)语句?

I have a MultipleChoiceField on a form holding car makes. I want to filter my database of cars to the makes that were checked but this causes a problem. How do I get all the Q(make=...) statements in dynamically?

我如何开始: ['value1','value2',...]

我想要结束: Q(col ='value1')| Q(col ='value2')| ...

我有其他几种方法。我已经尝试为每个make添加查询,并以相反的方式执行(执行多个排除),但它们都很慢。

I've couple of other methods. I've tried appending querysets for each make and doing it the other way around (doing multiple excludes) but they were both really slow.

推荐答案

你尝试过: Model.objects.filter(make__in = list_of_makes)

list_of_makes是什么由 MultipleChoiceField返回

The list_of_makes is what is returned by the MultipleChoiceField

请参阅 __中的 __的查询集参考中的/ dev / ref / models / querysets /#在

See the query set reference about the __in operator.

这篇关于Django动态OR查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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