summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTim Graham <timograham@gmail.com>2017-05-05 18:35:08 -0400
committerGitHub <noreply@github.com>2017-05-05 18:35:08 -0400
commitd66378a8b2d48d41b56ba3622189493e4edf9e5a (patch)
treefc614f23ca4b01882b19de34ec55885204b2ad87
parent2874531ab59d46a48e32fb553ac86499778784bc (diff)
Fixed #28175 -- Fixed __in lookups on a foreign key when using the foreign key's parent model as the lookup value.
Thanks Simon Charette for review.
-rw-r--r--django/db/models/fields/related_lookups.py3
-rw-r--r--docs/releases/1.11.1.txt3
-rw-r--r--tests/model_inheritance_regress/tests.py6
3 files changed, 11 insertions, 1 deletions
diff --git a/django/db/models/fields/related_lookups.py b/django/db/models/fields/related_lookups.py
index 39b8f6e941..96eb8c8776 100644
--- a/django/db/models/fields/related_lookups.py
+++ b/django/db/models/fields/related_lookups.py
@@ -81,7 +81,8 @@ class RelatedIn(In):
AND)
return root_constraint.as_sql(compiler, connection)
else:
- if not getattr(self.rhs, 'has_select_fields', True):
+ if (not getattr(self.rhs, 'has_select_fields', True) and
+ not getattr(self.lhs.field.target_field, 'primary_key', False)):
self.rhs.clear_select_clause()
if (getattr(self.lhs.output_field, 'primary_key', False) and
self.lhs.output_field.model == self.rhs.model):
diff --git a/docs/releases/1.11.1.txt b/docs/releases/1.11.1.txt
index e76ed491e9..7b1b887fa9 100644
--- a/docs/releases/1.11.1.txt
+++ b/docs/releases/1.11.1.txt
@@ -97,3 +97,6 @@ Bugfixes
* Prevented hiding GDAL errors if it's not installed when using ``contrib.gis``
(:ticket:`28160`). (It's a required dependency as of Django 1.11.)
+
+* Fixed a regression causing ``__in`` lookups on a foreign key to fail when
+ using the foreign key's parent model as the lookup value (:ticket:`28175`).
diff --git a/tests/model_inheritance_regress/tests.py b/tests/model_inheritance_regress/tests.py
index 87ec11a5b1..0ecaedd090 100644
--- a/tests/model_inheritance_regress/tests.py
+++ b/tests/model_inheritance_regress/tests.py
@@ -472,6 +472,12 @@ class ModelInheritanceTest(TestCase):
jane = Supplier.objects.order_by("name").select_related("restaurant")[0]
self.assertEqual(jane.restaurant.name, "Craft")
+ def test_filter_with_parent_fk(self):
+ r = Restaurant.objects.create()
+ s = Supplier.objects.create(restaurant=r)
+ # The mismatch between Restaurant and Place is intentional (#28175).
+ self.assertSequenceEqual(Supplier.objects.filter(restaurant__in=Place.objects.all()), [s])
+
def test_ptr_accessor_assigns_db(self):
r = Restaurant.objects.create()
self.assertEqual(r.place_ptr._state.db, 'default')