[1.11.x] 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.
Backport of d66378a8b2
from master
This commit is contained in:
parent
73d4560a92
commit
f9a4593376
|
@ -81,7 +81,8 @@ class RelatedIn(In):
|
||||||
AND)
|
AND)
|
||||||
return root_constraint.as_sql(compiler, connection)
|
return root_constraint.as_sql(compiler, connection)
|
||||||
else:
|
else:
|
||||||
if getattr(self.rhs, '_forced_pk', False):
|
if (getattr(self.rhs, '_forced_pk', False) and
|
||||||
|
not getattr(self.lhs.field.target_field, 'primary_key', False)):
|
||||||
self.rhs.clear_select_clause()
|
self.rhs.clear_select_clause()
|
||||||
if (getattr(self.lhs.output_field, 'primary_key', False) and
|
if (getattr(self.lhs.output_field, 'primary_key', False) and
|
||||||
self.lhs.output_field.model == self.rhs.model):
|
self.lhs.output_field.model == self.rhs.model):
|
||||||
|
|
|
@ -97,3 +97,6 @@ Bugfixes
|
||||||
|
|
||||||
* Prevented hiding GDAL errors if it's not installed when using ``contrib.gis``
|
* 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.)
|
(: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`).
|
||||||
|
|
|
@ -474,6 +474,12 @@ class ModelInheritanceTest(TestCase):
|
||||||
jane = Supplier.objects.order_by("name").select_related("restaurant")[0]
|
jane = Supplier.objects.order_by("name").select_related("restaurant")[0]
|
||||||
self.assertEqual(jane.restaurant.name, "Craft")
|
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):
|
def test_ptr_accessor_assigns_db(self):
|
||||||
r = Restaurant.objects.create()
|
r = Restaurant.objects.create()
|
||||||
self.assertEqual(r.place_ptr._state.db, 'default')
|
self.assertEqual(r.place_ptr._state.db, 'default')
|
||||||
|
|
Loading…
Reference in New Issue