From 600628f8f0ab077c62223a54a6c8cde6cbbd1ed1 Mon Sep 17 00:00:00 2001 From: Simon Charette Date: Sun, 1 Sep 2019 14:25:28 -0400 Subject: [PATCH] Refs #28442 -- Adjusted related lookups handling of expression rhs. Expressions should never be prepared as other Lookup.get_prep_lookup implementations hint at by returning early on the presence of the resolve_expression attribute. The previous solution was only handling lookups against related fields pointing at AutoFields and would break for foreign keys to other fields. It was also causing bidirectional coupling between model fields and expressions which the method level import of OuterRef was a symptom of. --- django/db/models/fields/__init__.py | 4 ---- django/db/models/fields/related_lookups.py | 2 +- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py index 6e924a4adf..9ce46e3f73 100644 --- a/django/db/models/fields/__init__.py +++ b/django/db/models/fields/__init__.py @@ -2332,10 +2332,6 @@ class AutoFieldMixin: value = connection.ops.validate_autopk_value(value) return value - def get_prep_value(self, value): - from django.db.models.expressions import OuterRef - return value if isinstance(value, OuterRef) else super().get_prep_value(value) - def contribute_to_class(self, cls, name, **kwargs): assert not cls._meta.auto_field, ( "Model %s can't have more than one auto-generated field." diff --git a/django/db/models/fields/related_lookups.py b/django/db/models/fields/related_lookups.py index 12f7fb0b9b..c20e220141 100644 --- a/django/db/models/fields/related_lookups.py +++ b/django/db/models/fields/related_lookups.py @@ -101,7 +101,7 @@ class RelatedIn(In): class RelatedLookupMixin: def get_prep_lookup(self): - if not isinstance(self.lhs, MultiColSource) and self.rhs_is_direct_value(): + if not isinstance(self.lhs, MultiColSource) and not hasattr(self.rhs, 'resolve_expression'): # If we get here, we are dealing with single-column relations. self.rhs = get_normalized_value(self.rhs, self.lhs)[0] # We need to run the related field's get_prep_value(). Consider case