Note that in the last example the article has moved from John to Paul.

Related managers support field lookups as well.
The API automatically follows relationships as far as you need.
Use double underscores to separate relationships.
This works as many levels deep as you want. There’s no limit. For example:

>>> r.article_set.filter(headline__startswith='This')<QuerySet [<Article: This is a test>]># Find all Articles for any Reporter whose first name is "John".>>> Article.objects.filter(reporter__first_name='John')<QuerySet [<Article: John's second story>, <Article: This is a test>]>

Exact match is implied here:

>>> Article.objects.filter(reporter__first_name='John')<QuerySet [<Article: John's second story>, <Article: This is a test>]>

Query twice over the related field. This translates to an AND condition in the
WHERE clause:

>>> Article.objects.filter(reporter__first_name='John',reporter__last_name='Smith')<QuerySet [<Article: John's second story>, <Article: This is a test>]>

For the related lookup you can supply a primary key value or pass the related
object explicitly:

>>> Article.objects.filter(reporter__pk=1)<QuerySet [<Article: John's second story>, <Article: This is a test>]>>>> Article.objects.filter(reporter=1)<QuerySet [<Article: John's second story>, <Article: This is a test>]>>>> Article.objects.filter(reporter=r)<QuerySet [<Article: John's second story>, <Article: This is a test>]>>>> Article.objects.filter(reporter__in=[1,2]).distinct()<QuerySet [<Article: John's second story>, <Article: Paul's story>, <Article: This is a test>]>>>> Article.objects.filter(reporter__in=[r,r2]).distinct()<QuerySet [<Article: John's second story>, <Article: Paul's story>, <Article: This is a test>]>

You can also use a queryset instead of a literal list of instances:

>>> Article.objects.filter(reporter__in=Reporter.objects.filter(first_name='John')).distinct()<QuerySet [<Article: John's second story>, <Article: This is a test>]>

This document is for Django's development version, which can be significantly different from previous releases. For older releases, use the version selector floating in the bottom right corner of this page.