Skip to content

Commit

Permalink
[1.4.X] Fixed #18090 -- Applied filters when running prefetch_related…
Browse files Browse the repository at this point in the history
… backwards through a one-to-one relation. Backport of r17888 from trunk.

git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.4.X@17889 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
aaugustin committed Apr 10, 2012
1 parent 8adfdf0 commit 01dfe35
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
2 changes: 1 addition & 1 deletion django/db/models/fields/related.py
Expand Up @@ -239,7 +239,7 @@ def get_query_set(self, **db_hints):
def get_prefetch_query_set(self, instances): def get_prefetch_query_set(self, instances):
vals = set(instance._get_pk_val() for instance in instances) vals = set(instance._get_pk_val() for instance in instances)
params = {'%s__pk__in' % self.related.field.name: vals} 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), attrgetter(self.related.field.attname),
lambda obj: obj._get_pk_val(), lambda obj: obj._get_pk_val(),
True, True,
Expand Down
7 changes: 7 additions & 0 deletions tests/modeltests/prefetch_related/tests.py
@@ -1,7 +1,9 @@
from __future__ import with_statement, absolute_import from __future__ import with_statement, absolute_import


from django.contrib.contenttypes.models import ContentType from django.contrib.contenttypes.models import ContentType
from django.db import connection
from django.test import TestCase from django.test import TestCase
from django.test.utils import override_settings


from .models import (Author, Book, Reader, Qualification, Teacher, Department, from .models import (Author, Book, Reader, Qualification, Teacher, Department,
TaggedItem, Bookmark, AuthorAddress, FavoriteAuthors, AuthorWithAge, TaggedItem, Bookmark, AuthorAddress, FavoriteAuthors, AuthorWithAge,
Expand Down Expand Up @@ -356,10 +358,15 @@ def test_parent_link_prefetch(self):
with self.assertNumQueries(2): with self.assertNumQueries(2):
[a.author for a in AuthorWithAge.objects.prefetch_related('author')] [a.author for a in AuthorWithAge.objects.prefetch_related('author')]


@override_settings(DEBUG=True)
def test_child_link_prefetch(self): def test_child_link_prefetch(self):
with self.assertNumQueries(2): with self.assertNumQueries(2):
l = [a.authorwithage for a in Author.objects.prefetch_related('authorwithage')] 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()]) self.assertEqual(l, [a.authorwithage for a in Author.objects.all()])




Expand Down

0 comments on commit 01dfe35

Please sign in to comment.