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:
parent
999891bd80
commit
600628f8f0
|
@ -2332,10 +2332,6 @@ class AutoFieldMixin:
|
||||||
value = connection.ops.validate_autopk_value(value)
|
value = connection.ops.validate_autopk_value(value)
|
||||||
return 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):
|
def contribute_to_class(self, cls, name, **kwargs):
|
||||||
assert not cls._meta.auto_field, (
|
assert not cls._meta.auto_field, (
|
||||||
"Model %s can't have more than one auto-generated field."
|
"Model %s can't have more than one auto-generated field."
|
||||||
|
|
|
@ -101,7 +101,7 @@ class RelatedIn(In):
|
||||||
|
|
||||||
class RelatedLookupMixin:
|
class RelatedLookupMixin:
|
||||||
def get_prep_lookup(self):
|
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.
|
# If we get here, we are dealing with single-column relations.
|
||||||
self.rhs = get_normalized_value(self.rhs, self.lhs)[0]
|
self.rhs = get_normalized_value(self.rhs, self.lhs)[0]
|
||||||
# We need to run the related field's get_prep_value(). Consider case
|
# We need to run the related field's get_prep_value(). Consider case
|
||||||
|
|
Loading…
Reference in New Issue