From b9113ca81f553489001882ba95daa415d3652e81 Mon Sep 17 00:00:00 2001 From: Russell Keith-Magee Date: Thu, 12 Jun 2008 13:19:37 +0000 Subject: [PATCH] Fixed #7327 -- Added documentation and test case for defining subqueries. Thanks, Sebastian Noack. git-svn-id: http://code.djangoproject.com/svn/django/trunk@7625 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- docs/db-api.txt | 11 +++++++++++ tests/modeltests/many_to_one/models.py | 6 ++++++ 2 files changed, 17 insertions(+) diff --git a/docs/db-api.txt b/docs/db-api.txt index a15c45d37b5..c94eb00f9e6 100644 --- a/docs/db-api.txt +++ b/docs/db-api.txt @@ -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 ~~~~~~~~~~ diff --git a/tests/modeltests/many_to_one/models.py b/tests/modeltests/many_to_one/models.py index 53ad4466bb1..dfb17b8344d 100644 --- a/tests/modeltests/many_to_one/models.py +++ b/tests/modeltests/many_to_one/models.py @@ -175,6 +175,12 @@ False >>> Article.objects.filter(reporter__in=[r,r2]).distinct() [, , ] +# 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() +[, ] + # You need two underscores between "reporter" and "id" -- not one. >>> Article.objects.filter(reporter_id__exact=1) Traceback (most recent call last):