链接的QuerySet过滤器是否等同于使用Django ORM在单个过滤器中定义多个字段? [英] Are chained QuerySet filters equivalent to defining multiple fields in a single filter with the Django ORM?

查看:211
本文介绍了链接的QuerySet过滤器是否等同于使用Django ORM在单个过滤器中定义多个字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

过滤查询后,我想知道以下是否相当。

When filtering a queryset, I'm wondering if the following are equivalent.

User.objects.filter(username='josh').filter(email__startswith='josh')
User.objects.filter(username='josh', email__startswith='josh')

我无法想象生成的SQL在两者之间可能有什么不同。

I can't imagine how the generated SQL could be any different between the two. The documentation doesn't seem to mention any differences either.

推荐答案

您可以在shell中执行这些查询,并打印出生成的SQL如下:

You can execute those queries in the shell and print out the generated SQL like:

>>> print User.objects.filter(username='josh').filter(email__startswith='josh').query

我测试了一个类似你的查询,就像你在这里,生成的SQL代码没有区别。两个语句最终使用相同的WHERE语句。

I tested a similiar queries like you got here and there was no difference in the generated SQL code.Both statements end up using them same WHERE Statement.

无论您连接过滤器还是应用它们,都不会有任何差异。

Furhtermore it shouldnt make any difference in this case whether you chain the filters or apply them in one step.

但是,有些情况下过滤的顺序很重要。
看看 here 此处

But there are scenarios in which the order of filtering matters. Have a look here and here.

这篇关于链接的QuerySet过滤器是否等同于使用Django ORM在单个过滤器中定义多个字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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