Skip to content

Commit

Permalink
[5.0.x] Refs #35638 -- Avoided wrapping expressions with Value in _ge…
Browse files Browse the repository at this point in the history
…t_field_value_map() and renamed to _get_field_expression_map().

Backport of 91a0387 from main.
  • Loading branch information
shangxiao authored and sarahboyce committed Aug 5, 2024
1 parent c822ad6 commit e88ef6a
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
2 changes: 1 addition & 1 deletion django/contrib/postgres/constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ def __repr__(self):

def validate(self, model, instance, exclude=None, using=DEFAULT_DB_ALIAS):
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
)
replacements = {F(field): value for field, value in replacement_map.items()}
Expand Down
9 changes: 7 additions & 2 deletions django/db/models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1244,12 +1244,17 @@ def _get_next_or_previous_in_order(self, is_next):
setattr(self, cachename, obj)
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:
exclude = set()
meta = meta or self._meta
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
if field.name not in exclude and not field.generated
}
Expand Down
8 changes: 5 additions & 3 deletions django/db/models/constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def remove_sql(self, model, schema_editor):
return schema_editor._delete_check_sql(model, self.name)

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:
if not Q(self.check).check(against, using=using):
raise ValidationError(
Expand Down Expand Up @@ -423,7 +423,7 @@ def validate(self, model, instance, exclude=None, using=DEFAULT_DB_ALIAS):
return
replacements = {
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
).items()
}
Expand Down Expand Up @@ -454,7 +454,9 @@ def validate(self, model, instance, exclude=None, using=DEFAULT_DB_ALIAS):
code=self.violation_error_code,
)
else:
against = instance._get_field_value_map(meta=model._meta, exclude=exclude)
against = instance._get_field_expression_map(
meta=model._meta, exclude=exclude
)
try:
if (self.condition & Exists(queryset.filter(self.condition))).check(
against, using=using
Expand Down

0 comments on commit e88ef6a

Please sign in to comment.