summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJason Parrott <jason.parrott@gree.net>2016-03-17 22:45:00 +0900
committerTim Graham <timograham@gmail.com>2016-03-19 17:55:31 -0400
commit4e8c26531925227872ce29bc0cdee3329be4a216 (patch)
treee102def08cc90253b5d4fcec75e0073614e7212a
parent2d178b3d2cbed7d7a21cf0de3bd9da9330c90755 (diff)
[1.9.x] Fixed #26373 -- Fixed reverse lookup crash with a ForeignKey to_field in a subquery.
Backport of 4c1c93032f4a015cbb4b33958603d18ac43515b4 from master
-rw-r--r--django/db/models/query.py2
-rw-r--r--docs/releases/1.9.5.txt4
-rw-r--r--tests/queries/tests.py4
3 files changed, 9 insertions, 1 deletions
diff --git a/django/db/models/query.py b/django/db/models/query.py
index cb02085cc5..3787e34abc 100644
--- a/django/db/models/query.py
+++ b/django/db/models/query.py
@@ -1113,7 +1113,7 @@ class QuerySet(object):
# if they are set up to select only a single field.
if len(self._fields or self.model._meta.concrete_fields) > 1:
raise TypeError('Cannot use multi-field values as a filter value.')
- else:
+ elif self.model != field.model:
# If the query is used as a subquery for a ForeignKey with non-pk
# target field, make sure to select the target field in the subquery.
foreign_fields = getattr(field, 'foreign_related_fields', ())
diff --git a/docs/releases/1.9.5.txt b/docs/releases/1.9.5.txt
index 98dd1799a8..37157b3e56 100644
--- a/docs/releases/1.9.5.txt
+++ b/docs/releases/1.9.5.txt
@@ -30,3 +30,7 @@ Bugfixes
* Fixed a regression that caused ``collectstatic --clear`` to fail if the
storage doesn't implement ``path()`` (:ticket:`26297`).
+
+* Fixed a crash when using a reverse lookup with a subquery when a
+ ``ForeignKey`` has a ``to_field`` set to something other than the primary key
+ (:ticket:`26373`).
diff --git a/tests/queries/tests.py b/tests/queries/tests.py
index 7c306c9f15..05564a5909 100644
--- a/tests/queries/tests.py
+++ b/tests/queries/tests.py
@@ -2474,6 +2474,10 @@ class ToFieldTests(TestCase):
set(Eaten.objects.filter(food__in=Food.objects.filter(name='apple').values('eaten__meal'))),
set()
)
+ self.assertEqual(
+ set(Food.objects.filter(eaten__in=Eaten.objects.filter(meal='lunch'))),
+ {apple}
+ )
def test_reverse_in(self):
apple = Food.objects.create(name="apple")