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-users@googlegroups.com [mailto:django-users@googlegroups.com] On 
Behalf Of Mahendra Gaur
Sent: Monday, April 3, 2017 9:42 AM
To: django-users@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" 
<matthew.p...@iss.com<mailto:matthew.p...@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-users@googlegroups.com<mailto:django-users@googlegroups.com> 
[mailto:django-users@googlegroups.com<mailto:django-users@googlegroups.com>] On 
Behalf Of Mahendra Gaur
Sent: Sunday, April 2, 2017 9:39 AM
To: django-users@googlegroups.com<mailto:django-users@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+unsubscr...@googlegroups.com<mailto:django-users+unsubscr...@googlegroups.com>.
To post to this group, send email to 
django-users@googlegroups.com<mailto: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/CAExdeFaPbedyCOuO_PHsRO3GcVC_nAMxNm6JCHfsJ%2BCn6bdssg%40mail.gmail.com<https://groups.google.com/d/msgid/django-users/CAExdeFaPbedyCOuO_PHsRO3GcVC_nAMxNm6JCHfsJ%2BCn6bdssg%40mail.gmail.com?utm_medium=email&utm_source=footer>.

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+unsubscr...@googlegroups.com<mailto:django-users+unsubscr...@googlegroups.com>.
To post to this group, send email to 
django-users@googlegroups.com<mailto: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/008b9ec564814285939b4b3cc406bb14%40ISS1.ISS.LOCAL<https://groups.google.com/d/msgid/django-users/008b9ec564814285939b4b3cc406bb14%40ISS1.ISS.LOCAL?utm_medium=email&utm_source=footer>.

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+unsubscr...@googlegroups.com<mailto:django-users+unsubscr...@googlegroups.com>.
To post to this group, send email to 
django-users@googlegroups.com<mailto: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/CAExdeFb7Zs0vrMKT5m%3DtkPJB1rRboq%2B5FsTKZ9V6%3DqVkWTGZag%40mail.gmail.com<https://groups.google.com/d/msgid/django-users/CAExdeFb7Zs0vrMKT5m%3DtkPJB1rRboq%2B5FsTKZ9V6%3DqVkWTGZag%40mail.gmail.com?utm_medium=email&utm_source=footer>.
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+unsubscr...@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/a989baaf2c274386bd27593cd0fd1c88%40ISS1.ISS.LOCAL.
For more options, visit https://groups.google.com/d/optout.

Reply via email to