From 1d9ee181fe09a5c3784bbbf802cc522f11ff25ef Mon Sep 17 00:00:00 2001 From: Anssi Kääriäinen Date: Thu, 11 Feb 2016 08:39:37 +0200 Subject: [1.9.x] Fixed #26196 -- Made sure __in lookups use to_field as default. Thanks Simon Charette for the test. Backport of 46ecfb9b3a11a360724e3375ba78c33c46d6a992 from master --- django/db/models/query.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'django/db/models/query.py') diff --git a/django/db/models/query.py b/django/db/models/query.py index 852a5ba0dd..cb02085cc5 100644 --- a/django/db/models/query.py +++ b/django/db/models/query.py @@ -1107,12 +1107,18 @@ class QuerySet(object): for field, objects in other._known_related_objects.items(): self._known_related_objects.setdefault(field, {}).update(objects) - def _prepare(self): + def _prepare(self, field): if self._fields is not None: # values() queryset can only be used as nested queries # 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: + # 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', ()) + if len(foreign_fields) == 1 and not foreign_fields[0].primary_key: + return self.values(foreign_fields[0].name) return self def _as_sql(self, connection): -- cgit v1.3