1
0
mirror of https://github.com/django/django.git synced 2024-12-22 17:16:24 +00:00

Refs #35638 -- Avoided wrapping expressions with Value in _get_field_value_map() and renamed to _get_field_expression_map().

This commit is contained in:
David Sanders 2024-08-05 08:20:48 +02:00 committed by Sarah Boyce
parent 304d256674
commit 91a038754b
3 changed files with 13 additions and 6 deletions

View File

@ -178,7 +178,7 @@ class ExclusionConstraint(BaseConstraint):
def validate(self, model, instance, exclude=None, using=DEFAULT_DB_ALIAS): def validate(self, model, instance, exclude=None, using=DEFAULT_DB_ALIAS):
queryset = model._default_manager.using(using) queryset = model._default_manager.using(using)
replacement_map = instance._get_field_value_map( replacement_map = instance._get_field_expression_map(
meta=model._meta, exclude=exclude meta=model._meta, exclude=exclude
) )
replacements = {F(field): value for field, value in replacement_map.items()} replacements = {F(field): value for field, value in replacement_map.items()}

View File

@ -1333,12 +1333,17 @@ class Model(AltersData, metaclass=ModelBase):
setattr(self, cachename, obj) setattr(self, cachename, obj)
return getattr(self, cachename) return getattr(self, cachename)
def _get_field_value_map(self, meta, exclude=None): def _get_field_expression_map(self, meta, exclude=None):
if exclude is None: if exclude is None:
exclude = set() exclude = set()
meta = meta or self._meta meta = meta or self._meta
field_map = { field_map = {
field.name: Value(getattr(self, field.attname), field) field.name: (
value
if (value := getattr(self, field.attname))
and hasattr(value, "resolve_expression")
else Value(value, field)
)
for field in meta.local_concrete_fields for field in meta.local_concrete_fields
if field.name not in exclude and not field.generated if field.name not in exclude and not field.generated
} }

View File

@ -241,7 +241,7 @@ class CheckConstraint(BaseConstraint):
return schema_editor._delete_check_sql(model, self.name) return schema_editor._delete_check_sql(model, self.name)
def validate(self, model, instance, exclude=None, using=DEFAULT_DB_ALIAS): def validate(self, model, instance, exclude=None, using=DEFAULT_DB_ALIAS):
against = instance._get_field_value_map(meta=model._meta, exclude=exclude) against = instance._get_field_expression_map(meta=model._meta, exclude=exclude)
try: try:
if not Q(self.condition).check(against, using=using): if not Q(self.condition).check(against, using=using):
raise ValidationError( raise ValidationError(
@ -638,7 +638,7 @@ class UniqueConstraint(BaseConstraint):
return return
replacements = { replacements = {
F(field): value F(field): value
for field, value in instance._get_field_value_map( for field, value in instance._get_field_expression_map(
meta=model._meta, exclude=exclude meta=model._meta, exclude=exclude
).items() ).items()
} }
@ -668,7 +668,9 @@ class UniqueConstraint(BaseConstraint):
code=self.violation_error_code, code=self.violation_error_code,
) )
else: else:
against = instance._get_field_value_map(meta=model._meta, exclude=exclude) against = instance._get_field_expression_map(
meta=model._meta, exclude=exclude
)
try: try:
if (self.condition & Exists(queryset.filter(self.condition))).check( if (self.condition & Exists(queryset.filter(self.condition))).check(
against, using=using against, using=using