混合使用 filter() 和 Q 对象的 Django ORM 查询
我希望创建一个稍微复杂的查询,使用原始 SQL 可以相当轻松地编写该查询。 以下是原始查询的示例:
从销售中选择我的字段,其中 is_paid = False OR status = 'toship' AND otherfield = 'FOO' AND anotherfield = 'BAR'
这很简单,它会生成 is_paid = False 的所有结果,然后为 my 生成第二个结果集AND 匹配。
现在我了解了 Q 对象,了解了过滤,但我似乎无法完全理解如何在 Django ORM 中干净地实现这一点。
有小费吗?
谢谢
I'm looking to create a slightly more complex query that is written fairly easily using raw SQL. Here's an example of the query in raw:
SELECT my,fields FROM sales WHERE is_paid = False OR status = 'toship' AND otherfield = 'FOO' AND anotherfield = 'BAR'
This is simple, it generates all the results that are is_paid = False and then a second result set for my AND matches.
Now I know about Q objects, I know about filtering but I can't seem to wrap my mind around how to achieve this in the Django ORM cleanly.
Any tips?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以继续以某种动态的方式构建您的 Q 对象。
例子:
You can keep building your Q object in a somewhat dynamic fashion.
Example:
尽管 googletorp 是正确的,您不能使用字符串动态构造查询,但您可以使用字典参数来实现。 类似于:
其中 mydict1 和 2 的形式为:
等等。
Although googletorp is right that you can't construct the query dynamically with a string, you can do it with dictionary parameters. Something like:
where mydict1 and 2 are of the form:
etc.
这是进行动态“OR”查询的好方法:
如果您想使用“AND”:
This is a great way to do dynamic "OR" querying:
if you want to use "AND":
像这样的事情应该有效:
编辑:
您无法像构造包含要在完成时执行的 SQL 语句的字符串一样动态创建查询。 如果你想这样做,我建议使用 if 状态、函数或最适合你的用例的东西:
这可能更复杂,但我相信你明白这个想法。
Something like this should work:
Edit:
You can't create the query dynamically the same way you can construct a string containing a SQL statement to be executed when completed. If you want to do this, I would suggest using an if state, function or what suits your use case best:
This could be more complex, but I'm sure you get the idea.