Are the following two calls resolved to the equivalent SQL query in Django?
Chaining multiple calls
Model.objects \
.filter(arg1=foo) \
.filter(arg2=bar) \
...
Wrapping all the args together:
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.
Update:
Disregard this answer. See this better, correct answer.
Thanks @Sam for the heads up.
Old Answer:
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
filterconditions areANDed together in the query.One way to achieve readability is to use a dictionary for collecting all filter conditions. For e.g.