From b107421acfc4046ffaa799aceef2c3b4877207f2 Mon Sep 17 00:00:00 2001 From: Loic Bistuer Date: Wed, 13 Nov 2013 11:42:12 +0700 Subject: [1.6.x] Fixed #21410 -- prefetch_related() for ForeignKeys with related_name='+' Regression introduced by commit 9777442. Thanks to trac username troygrosfield for the report and test case. Backpatch of cb83448891f2920c926f61826d8eae4051a3d8f2 from master. Conflicts: tests/prefetch_related/models.py --- django/db/models/fields/related.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) (limited to 'django') diff --git a/django/db/models/fields/related.py b/django/db/models/fields/related.py index 2813a39567..d50c72d66c 100644 --- a/django/db/models/fields/related.py +++ b/django/db/models/fields/related.py @@ -273,7 +273,17 @@ class ReverseSingleRelatedObjectDescriptor(six.with_metaclass(RenameRelatedObjec 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' % related_field.name: set(instance_attr(inst)[0] for inst in instances)} + else: + 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. -- cgit v1.3