These two filtering styles are equivalent in most cases, but when the query on objects is based on ForeignKey or ManyToManyField, they are slightly different.
Examples from the documentation .
model
An Entry Blog is a one-to-many relationship.
from django.db import models class Blog(models.Model): ... class Entry(models.Model): blog = models.ForeignKey(Blog) headline = models.CharField(max_length=255) pub_date = models.DateField() ...
objects
Assuming there is a blog and input objects here.

inquiries
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)
For the first request (one filter one), it matches only blog1.
For the second request (with code filters one), it filters blog1 and blog2.
The first filter restricts the set of queries for blog1, blog2, and blog5; the second filter restricts the set of blogs beyond blog1 and blog2.
And you must understand that
We filter Blog elements with each filter statement, not Entry elements.
So, this is not the same, because Blog and Entry are a multi-valued relationship.
Link: https://docs.djangoproject.com/en/1.8/topics/db/queries/#spanning-multi-valued-relationships
If something is wrong, please correct me.
Edit: Changed v1.6 to v1.8, since links 1.6 are no longer available.
Kevin_wyx Jan 31 '15 at 16:03 2015-01-31 16:03
source share