Skip to content

Commit

Permalink
Fixed #7327 -- Added documentation and test case for defining subquer…
Browse files Browse the repository at this point in the history
…ies. Thanks, Sebastian Noack.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@7625 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
freakboy3742 committed Jun 12, 2008
1 parent ac5b9f5 commit b9113ca
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
11 changes: 11 additions & 0 deletions docs/db-api.txt
Expand Up @@ -1373,6 +1373,17 @@ SQL equivalent::

SELECT ... WHERE id IN (1, 3, 4);

You can also use a queryset to dynamically evaluate the list of values
instead of providing a list of literal values. The queryset must be
reduced to a list of individual values using the ``values()`` method,
and then converted into a query using the ``query`` attribute::

Entry.objects.filter(blog__in=Blog.objects.filter(name__contains='Cheddar').values('pk').query)

This queryset will be evaluated as subselect statement::

SELET ... WHERE blog.id IN (SELECT id FROM ... WHERE NAME LIKE '%Cheddar%')

startswith
~~~~~~~~~~

Expand Down
6 changes: 6 additions & 0 deletions tests/modeltests/many_to_one/models.py
Expand Up @@ -175,6 +175,12 @@ class Meta:
>>> Article.objects.filter(reporter__in=[r,r2]).distinct()
[<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.
# The queryset must be reduced to a list of values using values(),
# then converted into a query
>>> Article.objects.filter(reporter__in=Reporter.objects.filter(first_name='John').values('pk').query).distinct()
[<Article: John's second story>, <Article: This is a test>]
# You need two underscores between "reporter" and "id" -- not one.
>>> Article.objects.filter(reporter_id__exact=1)
Traceback (most recent call last):
Expand Down

0 comments on commit b9113ca

Please sign in to comment.