1
0
mirror of https://github.com/django/django.git synced 2025-10-11 15:59:11 +00:00

Refs #27222 -- Deduplicated db_returning fields in Model.save().

Follow-up to 94680437a45a71c70ca8bd2e68b72aa1e2eff337.
This commit is contained in:
Jacob Walls 2025-09-16 10:57:32 -04:00
parent 8c621e9642
commit e059bbec96
2 changed files with 12 additions and 1 deletions

View File

@ -1153,6 +1153,7 @@ class Model(AltersData, metaclass=ModelBase):
getattr(self, field.attname) if raw else field.pre_save(self, False) getattr(self, field.attname) if raw else field.pre_save(self, False)
) )
if hasattr(value, "resolve_expression"): if hasattr(value, "resolve_expression"):
if field not in returning_fields:
returning_fields.append(field) returning_fields.append(field)
elif field.db_returning: elif field.db_returning:
returning_fields.remove(field) returning_fields.remove(field)

View File

@ -42,6 +42,16 @@ class ReturningValuesTests(TestCase):
), ),
captured_queries[-1]["sql"], captured_queries[-1]["sql"],
) )
self.assertEqual(
captured_queries[-1]["sql"]
.split("RETURNING ")[1]
.count(
connection.ops.quote_name(
ReturningModel._meta.get_field("created").column
),
),
1,
)
self.assertTrue(obj.pk) self.assertTrue(obj.pk)
self.assertIsInstance(obj.created, datetime.datetime) self.assertIsInstance(obj.created, datetime.datetime)