filter chaining v/s filter with multiple arguments.

1,436 views
Skip to first unread message

Mahendra Gaur

unread,
Apr 2, 2017, 2:41:29 PM4/2/17
to django...@googlegroups.com
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

Todor Velichkov

unread,
Apr 3, 2017, 8:09:03 AM4/3/17
to Django users
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

Mahendra Gaur

unread,
Apr 3, 2017, 11:33:28 AM4/3/17
to django...@googlegroups.com
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

--
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...@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.

Matthew Pava

unread,
Apr 3, 2017, 1:26:02 PM4/3/17
to django...@googlegroups.com

Hi Mahendra,

You can view the SQL that is generated by using the query attribute on the QuerySet.

q = Blog.objects.filter(entry__headline__contains='Lennon', entry__pub_date__year=2008)
print(str(q.query))

 

That should help in understanding what is going on.

Best wishes!

--

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.

Mahendra Gaur

unread,
Apr 3, 2017, 2:42:21 PM4/3/17
to django...@googlegroups.com
Thanks for reply.

I have tried q.query on both statements but both are giving same query. I had tried this even before initiating this mail chain, and this was the point where I got confused.



Thanks and regards,
Mahendra Gaur


On 3 Apr 2017 6:55 p.m., "Matthew Pava" <Matthe...@iss.com> wrote:

Hi Mahendra,

You can view the SQL that is generated by using the query attribute on the QuerySet.

q = Blog.objects.filter(entry__headline__contains='Lennon', entry__pub_date__year=2008)
print(str(q.query))

 

That should help in understanding what is going on.

Best wishes!

 

From: django...@googlegroups.com [mailto:django-users@googlegroups.com] On Behalf Of Mahendra Gaur
Sent: Sunday, April 2, 2017 9:39 AM
To: django...@googlegroups.com
Subject: filter chaining v/s filter with multiple arguments.

 

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+unsubscribe@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

--
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...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

Matthew Pava

unread,
Apr 3, 2017, 3:30:30 PM4/3/17
to django...@googlegroups.com

Hi Mahendra,

Then that would suggest that there really is no difference for the purposes of generating the SQL statement in chaining filters as opposed to adding multiple arguments in one filter, wouldn’t it?

 

So you might then wonder why do any chaining?  Well, sometimes we assign a filter to a variable and then we want to add further filters based on some condition in the code.  We can simply chain the filter in that situation rather than create a dict of arguments to the filter.  I suppose it is a matter of style and preference.  Since QuerySets are evaluated lazily, there is no performance degradation with either option.  I personally prefer chaining the filters together.

 

q = Blog.objects.filter(entry__headline__contains='Lennon', entry__pub_date__year=2008)
if select_only_january:
     q = q.filter(entry__pub_date__month=0)

 

--or create a keyword dictionary--

 

kwargs = { 'entry__headline__contains': 'Lennon', 'entry__pub_date__year': 2008 }
if select_only_january:
     kwargs['entry__pub_date__month'] =0
q = Blog.objects.filter(**kwargs)

There are other ways of doing it to with Q objects.

I hope this helps.

 

 

From: django...@googlegroups.com [mailto:django...@googlegroups.com] On Behalf Of Mahendra Gaur
Sent: Monday, April 3, 2017 9:42 AM
To: django...@googlegroups.com
Subject: RE: filter chaining v/s filter with multiple arguments.

 

Thanks for reply.

 

I have tried q.query on both statements but both are giving same query. I had tried this even before initiating this mail chain, and this was the point where I got confused.

 

 

 

Thanks and regards,

Mahendra Gaur

 

On 3 Apr 2017 6:55 p.m., "Matthew Pava" <Matthe...@iss.com> wrote:

Hi Mahendra,

You can view the SQL that is generated by using the query attribute on the QuerySet.

q = Blog.objects.filter(entry__headline__contains='Lennon', entry__pub_date__year=2008)
print(str(q.query))

 

That should help in understanding what is going on.

Best wishes!

 

From: django...@googlegroups.com [mailto:django...@googlegroups.com] On Behalf Of Mahendra Gaur
Sent: Sunday, April 2, 2017 9:39 AM
To: django...@googlegroups.com
Subject: filter chaining v/s filter with multiple arguments.

 

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.

--

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.

 

--

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.

Todor Velichkov

unread,
Apr 3, 2017, 8:05:56 PM4/3/17
to Django users
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.

Mahendra Gaur

unread,
Apr 4, 2017, 11:03:45 AM4/4/17
to django...@googlegroups.com
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


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...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

Todor Velichkov

unread,
Apr 4, 2017, 12:25:33 PM4/4/17
to Django users
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

Matthew Pava

unread,
Apr 4, 2017, 1:32:16 PM4/4/17
to django...@googlegroups.com

Well, this is a time I wish I could delete my previous message from existence.  After looking at my own code, I realize now that I didn’t use much chaining; rather, I used Q objects and the & operator.

Mahendra Gaur

unread,
Apr 5, 2017, 7:07:13 AM4/5/17
to django...@googlegroups.com
Thanks everyone.

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




Thanks and Regards,
Mahendra

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...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
Reply all
Reply to author
Forward
0 new messages