Are the following two calls resolved to the equivalent SQL query in Django?
以下两个调用是否已解析为Django中的等效SQL查询?
Chaining multiple calls
链接多个电话
Model.objects \
.filter(arg1=foo) \
.filter(arg2=bar) \
...
Wrapping all the args together:
将所有的args包装在一起:
Model.objects \
.filter(arg1=foo, arg2=bar)
I'd like code to be readable (there are MANY more filter calls than I've shown), but only if there's no sacrifice to performance.
我希望代码可读(有很多过滤器调用比我所示),但前提是没有牺牲性能。
2 个解决方案
#1
11
Update:
Disregard this answer. See this better, correct answer. Thanks @Sam for the heads up.
无视这个答案。看到这个更好,正确的答案。谢谢@Sam的提醒。
Old Answer:
Are the following two calls resolved to the equivalent SQL query in Django?
以下两个调用是否已解析为Django中的等效SQL查询?
Short answer: yes. They will generate equivalent queries.
简短回答:是的。他们将生成等效的查询。
I verified this with a model I am using. the queries produced are functionally identical. The different filter
conditions are AND
ed together in the query.
我用我正在使用的模型验证了这一点。生成的查询在功能上是相同的。在查询中将不同的过滤条件进行AND运算。
I'd like code to be readable (there are MANY more filter calls than I've shown), but only if there's no sacrifice to performance.
我希望代码可读(有很多过滤器调用比我所示),但前提是没有牺牲性能。
One way to achieve readability is to use a dictionary for collecting all filter conditions. For e.g.
实现可读性的一种方法是使用字典来收集所有过滤条件。对于例如
conditions = dict(arg1 = foo, arg2 = bar, ....)
conditions.update(argN = baz)
Model.objects.filter(**conditions)
#2
6
In addition to Manoj's answer, here's how you can analyze the sql generated for a QuerySet
object:
除了Manoj的答案,这里是你如何分析为QuerySet对象生成的sql:
result1 = SomeModel.objects.filter(field1=100, field2=200)
print "Result1", results1.query
result2 = SomeModel.objects.filter(field1=100).filter(field2=200)
print "Result2", result2.query
#1
11
Update:
Disregard this answer. See this better, correct answer. Thanks @Sam for the heads up.
无视这个答案。看到这个更好,正确的答案。谢谢@Sam的提醒。
Old Answer:
Are the following two calls resolved to the equivalent SQL query in Django?
以下两个调用是否已解析为Django中的等效SQL查询?
Short answer: yes. They will generate equivalent queries.
简短回答:是的。他们将生成等效的查询。
I verified this with a model I am using. the queries produced are functionally identical. The different filter
conditions are AND
ed together in the query.
我用我正在使用的模型验证了这一点。生成的查询在功能上是相同的。在查询中将不同的过滤条件进行AND运算。
I'd like code to be readable (there are MANY more filter calls than I've shown), but only if there's no sacrifice to performance.
我希望代码可读(有很多过滤器调用比我所示),但前提是没有牺牲性能。
One way to achieve readability is to use a dictionary for collecting all filter conditions. For e.g.
实现可读性的一种方法是使用字典来收集所有过滤条件。对于例如
conditions = dict(arg1 = foo, arg2 = bar, ....)
conditions.update(argN = baz)
Model.objects.filter(**conditions)
#2
6
In addition to Manoj's answer, here's how you can analyze the sql generated for a QuerySet
object:
除了Manoj的答案,这里是你如何分析为QuerySet对象生成的sql:
result1 = SomeModel.objects.filter(field1=100, field2=200)
print "Result1", results1.query
result2 = SomeModel.objects.filter(field1=100).filter(field2=200)
print "Result2", result2.query