Skip to content

Commit e88ef6a

Browse files
shangxiaosarahboyce
authored andcommitted
[5.0.x] Refs #35638 -- Avoided wrapping expressions with Value in _get_field_value_map() and renamed to _get_field_expression_map().
Backport of 91a0387 from main.
1 parent c822ad6 commit e88ef6a

File tree

3 files changed

+13
-6
lines changed

3 files changed

+13
-6
lines changed

django/contrib/postgres/constraints.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ def __repr__(self):
183183

184184
def validate(self, model, instance, exclude=None, using=DEFAULT_DB_ALIAS):
185185
queryset = model._default_manager.using(using)
186-
replacement_map = instance._get_field_value_map(
186+
replacement_map = instance._get_field_expression_map(
187187
meta=model._meta, exclude=exclude
188188
)
189189
replacements = {F(field): value for field, value in replacement_map.items()}

django/db/models/base.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1244,12 +1244,17 @@ def _get_next_or_previous_in_order(self, is_next):
12441244
setattr(self, cachename, obj)
12451245
return getattr(self, cachename)
12461246

1247-
def _get_field_value_map(self, meta, exclude=None):
1247+
def _get_field_expression_map(self, meta, exclude=None):
12481248
if exclude is None:
12491249
exclude = set()
12501250
meta = meta or self._meta
12511251
field_map = {
1252-
field.name: Value(getattr(self, field.attname), field)
1252+
field.name: (
1253+
value
1254+
if (value := getattr(self, field.attname))
1255+
and hasattr(value, "resolve_expression")
1256+
else Value(value, field)
1257+
)
12531258
for field in meta.local_concrete_fields
12541259
if field.name not in exclude and not field.generated
12551260
}

django/db/models/constraints.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ def remove_sql(self, model, schema_editor):
124124
return schema_editor._delete_check_sql(model, self.name)
125125

126126
def validate(self, model, instance, exclude=None, using=DEFAULT_DB_ALIAS):
127-
against = instance._get_field_value_map(meta=model._meta, exclude=exclude)
127+
against = instance._get_field_expression_map(meta=model._meta, exclude=exclude)
128128
try:
129129
if not Q(self.check).check(against, using=using):
130130
raise ValidationError(
@@ -423,7 +423,7 @@ def validate(self, model, instance, exclude=None, using=DEFAULT_DB_ALIAS):
423423
return
424424
replacements = {
425425
F(field): value
426-
for field, value in instance._get_field_value_map(
426+
for field, value in instance._get_field_expression_map(
427427
meta=model._meta, exclude=exclude
428428
).items()
429429
}
@@ -454,7 +454,9 @@ def validate(self, model, instance, exclude=None, using=DEFAULT_DB_ALIAS):
454454
code=self.violation_error_code,
455455
)
456456
else:
457-
against = instance._get_field_value_map(meta=model._meta, exclude=exclude)
457+
against = instance._get_field_expression_map(
458+
meta=model._meta, exclude=exclude
459+
)
458460
try:
459461
if (self.condition & Exists(queryset.filter(self.condition))).check(
460462
against, using=using

0 commit comments

Comments
 (0)