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)
--
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.
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAExdeFaPbedyCOuO_PHsRO3GcVC_nAMxNm6JCHfsJ%2BCn6bdssg%40mail.gmail.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!
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAExdeFaPbedyCOuO_PHsRO3GcVC_nAMxNm6JCHfsJ%2BCn6bdssg%40mail.gmail.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...@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.
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAExdeFaPbedyCOuO_PHsRO3GcVC_nAMxNm6JCHfsJ%2BCn6bdssg%40mail.gmail.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/008b9ec564814285939b4b3cc406bb14%40ISS1.ISS.LOCAL.
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/CAExdeFb7Zs0vrMKT5m%3DtkPJB1rRboq%2B5FsTKZ9V6%3DqVkWTGZag%40mail.gmail.com.
Blog.objects.filter(entry__headline__contains='Lennon', entry__pub_date__year=2008)
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 '\'
)
select all blogs that contain entries with both “Lennon” in the headline and that were published in 2008 (the same entry satisfying both conditions)
Blog.objects.filter(entry__headline__contains='Lennon').filter(entry__pub_date__year=2008)
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 '\'
)
select all blogs that contain an entry with “Lennon” in the headline as well as an entry that was published in 2008
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.
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 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/e25257a6-96b8-4c88-8699-32b6a0b38d0d%40googlegroups.com.
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
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/e25257a6-96b8-4c88-8699-32b6a0b38d0d%40googlegroups.com.
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/ddc7e8ad-d65c-42ae-8b0f-61bc62499765%40googlegroups.com.