Wednesday, April 5, 2017

Re: filter chaining v/s filter with multiple arguments.

Thanks everyone.

Now I have got a clear idea of how these both statements differ to each other.




Thanks and Regards,
Mahendra

On Tue, Apr 4, 2017 at 5:55 PM, Todor Velichkov <todorvelichkov89@gmail.com> wrote:
I didn't use anything special, just print on queryset.query

qs1 = Blog.objects.filter(entry__headline__contains='Lennon', entry__pub_date__year=2008)
qs2
= Blog.objects.filter(entry__headline__contains='Lennon').filter(entry__pub_date__year=2008)

print qs1.query
print qs2.query


On Tuesday, April 4, 2017 at 2:03:45 PM UTC+3, Mahendra Gaur wrote:
Thanks Todor Velichkov.

It would be great if you let me know how did you get these queries (tool name etc).
Because when I was trying to get MySQL query using q.query, these both statements were giving same query.



Thanks and Regards,
Mahendra


On Tue, Apr 4, 2017 at 1:35 AM, Todor Velichkov <todorvel...@gmail.com> wrote:
Nope, they are not the same.

The first one:
Blog.objects.filter(entry__headline__contains='Lennon', entry__pub_date__year=2008)

Will produce the following SQL:
SELECT
   
"blog_blog"."id",
   
"blog_blog"."name",
   
"blog_blog"."tagline"
FROM
   
"blog_blog"
INNER JOIN
"blog_entry" ON ("blog_blog"."id" = "blog_entry"."blog_id")
WHERE
(
   
"blog_entry"."pub_date" BETWEEN 2008-01-01 AND 2008-12-31
    AND
"blog_entry"."headline" LIKE %Lennon% ESCAPE '\'
)

Which can be translated as:
select all blogs that contain entries with both "Lennon" in the headline and that were published in 2008 (the same entry satisfying both conditions)

 
While the second one:
Blog.objects.filter(entry__headline__contains='Lennon').filter(entry__pub_date__year=2008)

Will produce:
SELECT
   
"blog_blog"."id",
   
"blog_blog"."name",
   
"blog_blog"."tagline"
FROM
"blog_blog"
INNER JOIN
"blog_entry" ON ("blog_blog"."id" = "blog_entry"."blog_id")
INNER JOIN
"blog_entry" T3 ON ("blog_blog"."id" = T3."blog_id")
WHERE
(
    T3
."pub_date" BETWEEN 2008-01-01 AND 2008-12-31
    ABD
"blog_entry"."headline" LIKE %Lennon% ESCAPE '\'
)

Which can be translated as:
select all blogs that contain an entry with "Lennon" in the headline as well as an entry that was published in 2008

More from the docs:
Suppose there is only one blog that had both entries containing "Lennon" and entries from 2008, but that none of the entries from 2008 contained "Lennon". The first query would not return any blogs, but the second query would return that one blog.

In the second example, the first filter restricts the queryset to all those blogs linked to entries with "Lennon" in the headline. The second filter restricts the set of blogs further to those that are also linked to entries that were published in 2008. The entries selected by the second filter may or may not be the same as the entries in the first filter. We are filtering the Blog items with each filter statement, not the Entry items.

Sometimes we need several JOINs over the same table, sometimes we don't. And this is the way to achieve it. You want a single JOIN, put everything into a single .filter call, if you want multiple JOINs, use multiple .filter calls.

On Monday, April 3, 2017 at 2:33:28 PM UTC+3, Mahendra Gaur wrote:
Thank you for reply.

In case of second statement, why two JOIN are required each with single filter ?
As per my understanding whether it use one JOIN with both the filter or two JOIN each with single filter, both are same.
correct me if am wrong ?



Thanks and Regards,
Mahendra

On Mon, Apr 3, 2017 at 1:39 PM, Todor Velichkov <todorvel...@gmail.com> wrote:
The first one will use a single JOIN on the entry table, and will apply both filters to that table.
The second one will JOIN the entry table twice, and for every join will apply only a single filter


On Sunday, April 2, 2017 at 5:41:29 PM UTC+3, Mahendra Gaur wrote:
Hello everyone,

I am newbie to django. Now-a-days am reading django docs.
While reading models i got confusion that, What is the difference between filter chaining and filter with multiple arguments.
For example what is the diffrence between below two:

Blog.objects.filter(entry__headline__contains='Lennon', entry__pub_date__year=2008)
Blog.objects.filter(entry__headline__contains='Lennon').filter(entry__pub_date__year=2008)

can some one please help me to understand how these both works and most importantly what will be corresponding MYSQL queries for above two statements ?



Thanks and Regards,
Mahendra

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/19f4e103-0325-4f8e-bd03-502a1f2fe002%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/e25257a6-96b8-4c88-8699-32b6a0b38d0d%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/ddc7e8ad-d65c-42ae-8b0f-61bc62499765%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAExdeFbJomhVE%3D7V8vBGHJytHn3X6G8%2BYmB-JQaqB2Nv1aYWpw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment