Reputation: 42003
In Django, do the following two snippets produce the same underlying SQL query?
qs = MyModel.objects.filter(group=1, type=2)
and
qs = MyModel.objects.filter(group=1).filter(type=2)
Upvotes: 2
Views: 906
Reputation: 23871
It depends actually, on whether there are joins or spanned lookups, especially through M2M
relationship. For example
>>> print User.objects.filter(groups__gt=1).filter(groups__lt=2).query
SELECT "auth_user"."id", "auth_user"."username", "auth_user"."first_name", "auth_user"."last_name", "auth_user"."email", "auth_user"."password", "auth_user"."is_staff", "auth_user"."is_active", "auth_user"."is_superuser", "auth_user"."last_login", "auth_user"."date_joined" FROM "auth_user" INNER JOIN "auth_user_groups" ON ("auth_user"."id" = "auth_user_groups"."user_id") INNER JOIN "auth_user_groups" T4 ON ("auth_user"."id" = T4."user_id") WHERE ("auth_user_groups"."group_id" > 1 AND T4."group_id" < 2 )
>>> print User.objects.filter(groups__gt=1, groups__lt=2).query
SELECT "auth_user"."id", "auth_user"."username", "auth_user"."first_name", "auth_user"."last_name", "auth_user"."email", "auth_user"."password", "auth_user"."is_staff", "auth_user"."is_active", "auth_user"."is_superuser", "auth_user"."last_login", "auth_user"."date_joined" FROM "auth_user" INNER JOIN "auth_user_groups" ON ("auth_user"."id" = "auth_user_groups"."user_id") WHERE ("auth_user_groups"."group_id" < 2 AND "auth_user_groups"."group_id" > 1 )
Upvotes: 4
Reputation: 5540
Both are same and i even checked the sql query being generated.They are the same.
CreateCardTrack.objects.filter(email='vivek').filter(id=1)
>>> connection.queries
[{'time': '0.000', 'sql': u'SELECT `CreateCardTrack`.`id`, `CreateCardTrack`.`email`, ` CreateCardTrack`.`date` FROM `CreateCardTrack` WHERE (`CreateCardTrack`.`email` = vivek.s AND `CreateCardTrack`.`id` = 1 ) LIMIT 21'}]
>>> CreateCardTrack.objects.filter(email='vivek.s',id=1)
[<CreateCardTrack: CreateCardTrack object>]
>>> #SELECT `CreateCardTrack`.`id`, `CreateCardTrack`.`email`, `CreateCardTrack`.`date` FROM `CreateCardTrack` WHERE (`CreateCardTrack`.`email` = vivek.s AND `CreateCardTrack`.`id` = 1 ) LIMIT 21
Upvotes: 1
Reputation: 137310
Yes
QuerySet
sMore documentation on chaining QuerySet
's filter
s: https://docs.djangoproject.com/en/dev/topics/db/queries/#chaining-filters
There is some difference, however. With every filter()
method call you receive new QuerySet
object, so doing this call:
qs = Model.objects.filter(group=1, type=2)
seems to be wiser (in terms of performance and amount of code you need to write) than doing this call:
qs = Model.objects.filter(group=1).filter(type=2)
QuerySet
s are lazyAs in the title of this section, just getting QuerySet
in return does not mean the query has been executed on the database. It is just a container of the conditions that will be used to perform the query.
Documentation says:
QuerySet
s are lazy -- the act of creating aQuerySet
doesn't involve any database activity. You can stack filters together all day long, and Django won't actually run the query until theQuerySet
is evaluated.
Upvotes: 3