Skip to content
Browse files

[1.6.x] Fixed #21410 -- prefetch_related() for ForeignKeys with relat…


Regression introduced by commit 9777442.

Thanks to trac username troygrosfield for the report and test case.

Backpatch of cb83448 from master.


  • Loading branch information...
loic authored and akaariai committed Nov 13, 2013
1 parent b6acc4f commit b107421acfc4046ffaa799aceef2c3b4877207f2
Showing with 53 additions and 2 deletions.
  1. +11 −1 django/db/models/fields/
  2. +15 −0 tests/prefetch_related/
  3. +27 −1 tests/prefetch_related/
@@ -273,7 +273,17 @@ def get_prefetch_queryset(self, instances):
rel_obj_attr = self.field.get_foreign_related_value
instance_attr = self.field.get_local_related_value
instances_dict = dict((instance_attr(inst), inst) for inst in instances)
query = {'%s__in' % self.field.related_query_name(): instances}
related_field = self.field.foreign_related_fields[0]

# FIXME: This will need to be revisited when we introduce support for
# composite fields. In the meantime we take this practical approach to
# solve a regression on 1.6 when the reverse manager in hidden
# (related_name ends with a '+'). Refs #21410.
if self.field.rel.is_hidden():
query = {'%s__in' % set(instance_attr(inst)[0] for inst in instances)}
query = {'%s__in' % self.field.related_query_name(): instances}

qs = self.get_queryset(instance=instances[0]).filter(**query)
# Since we're going to assign directly in the cache,
# we must manage the reverse relation cache manually.
@@ -215,3 +215,18 @@ class WordEntry(models.Model):

def __str__(self):
return "%s (%s)" % (,

## Ticket #21410: Regression when related_name="+"

class Author2(models.Model):
name = models.CharField(max_length=50, unique=True)
first_book = models.ForeignKey('Book', related_name='first_time_authors+')
favorite_books = models.ManyToManyField('Book', related_name='+')

def __str__(self):

class Meta:
ordering = ['id']
@@ -9,7 +9,7 @@
from .models import (Author, Book, Reader, Qualification, Teacher, Department,
TaggedItem, Bookmark, AuthorAddress, FavoriteAuthors, AuthorWithAge,
BookWithYear, BookReview, Person, House, Room, Employee, Comment,
LessonEntry, WordEntry)
LessonEntry, WordEntry, Author2)

class PrefetchRelatedTests(TestCase):
@@ -651,3 +651,29 @@ def setUp(self):

def test_bug(self):
list(WordEntry.objects.prefetch_related('lesson_entry', 'lesson_entry__wordentry_set'))

class Ticket21410Tests(TestCase):

def setUp(self):
self.book1 = Book.objects.create(title="Poems")
self.book2 = Book.objects.create(title="Jane Eyre")
self.book3 = Book.objects.create(title="Wuthering Heights")
self.book4 = Book.objects.create(title="Sense and Sensibility")

self.author1 = Author2.objects.create(name="Charlotte",
self.author2 = Author2.objects.create(name="Anne",
self.author3 = Author2.objects.create(name="Emily",
self.author4 = Author2.objects.create(name="Jane",

self.author1.favorite_books.add(self.book1, self.book2, self.book3)

def test_bug(self):
list(Author2.objects.prefetch_related('first_book', 'favorite_books'))

0 comments on commit b107421

Please sign in to comment.
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.