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.
This commit is contained in:
Simon Charette 2019-09-01 14:25:28 -04:00 committed by Mariusz Felisiak
parent 999891bd80
commit 600628f8f0
2 changed files with 1 additions and 5 deletions

View File

@ -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."

View File

@ -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