summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAymeric Augustin <aymeric.augustin@m4x.org>2012-04-10 06:04:53 +0000
committerAymeric Augustin <aymeric.augustin@m4x.org>2012-04-10 06:04:53 +0000
commit1f11069aa59df03157ed90788c7759df77ec4377 (patch)
tree33df94eadc8018a02f0e630c921900537ba54366
parentf195f1ed2481d34ff56df36cf46ad7d377f2af93 (diff)
Fixed #18090 -- Applied filters when running prefetch_related backwards through a one-to-one relation.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@17888 bcc190cf-cafb-0310-a4f2-bffc1f526a37
-rw-r--r--django/db/models/fields/related.py2
-rw-r--r--tests/modeltests/prefetch_related/tests.py7
2 files changed, 8 insertions, 1 deletions
diff --git a/django/db/models/fields/related.py b/django/db/models/fields/related.py
index e23c7dc9b0..7034d34867 100644
--- a/django/db/models/fields/related.py
+++ b/django/db/models/fields/related.py
@@ -239,7 +239,7 @@ class SingleRelatedObjectDescriptor(object):
def get_prefetch_query_set(self, instances):
vals = set(instance._get_pk_val() for instance in instances)
params = {'%s__pk__in' % self.related.field.name: vals}
- return (self.get_query_set(instance=instances[0]),
+ return (self.get_query_set(instance=instances[0]).filter(**params),
attrgetter(self.related.field.attname),
lambda obj: obj._get_pk_val(),
True,
diff --git a/tests/modeltests/prefetch_related/tests.py b/tests/modeltests/prefetch_related/tests.py
index 135b5b8ad2..035ae4aa10 100644
--- a/tests/modeltests/prefetch_related/tests.py
+++ b/tests/modeltests/prefetch_related/tests.py
@@ -1,7 +1,9 @@
from __future__ import absolute_import
from django.contrib.contenttypes.models import ContentType
+from django.db import connection
from django.test import TestCase
+from django.test.utils import override_settings
from .models import (Author, Book, Reader, Qualification, Teacher, Department,
TaggedItem, Bookmark, AuthorAddress, FavoriteAuthors, AuthorWithAge,
@@ -356,10 +358,15 @@ class MultiTableInheritanceTest(TestCase):
with self.assertNumQueries(2):
[a.author for a in AuthorWithAge.objects.prefetch_related('author')]
+ @override_settings(DEBUG=True)
def test_child_link_prefetch(self):
with self.assertNumQueries(2):
l = [a.authorwithage for a in Author.objects.prefetch_related('authorwithage')]
+ # Regression for #18090: the prefetching query must include an IN clause.
+ self.assertIn('authorwithage', connection.queries[-1]['sql'])
+ self.assertIn(' IN ', connection.queries[-1]['sql'])
+
self.assertEqual(l, [a.authorwithage for a in Author.objects.all()])