mirror of
https://github.com/django/django.git
synced 2025-03-09 17:02:43 +00:00
[5.0.x] Fixed #35350 -- Fixed save() with pk set on models with GeneratedFields.
Thanks Matt Hegarty for the report and Simon Charette and Natalia Bidart for the reviews. Regression in f333e35. Backport of 8b53560eea9f10a1271d3bdf765dc6f969c7d9d5 from main.
This commit is contained in:
parent
58061fd2b4
commit
f075a19e85
@ -993,12 +993,16 @@ class Model(AltersData, metaclass=ModelBase):
|
||||
for a single table.
|
||||
"""
|
||||
meta = cls._meta
|
||||
non_pks = [f for f in meta.local_concrete_fields if not f.primary_key]
|
||||
non_pks_non_generated = [
|
||||
f
|
||||
for f in meta.local_concrete_fields
|
||||
if not f.primary_key and not f.generated
|
||||
]
|
||||
|
||||
if update_fields:
|
||||
non_pks = [
|
||||
non_pks_non_generated = [
|
||||
f
|
||||
for f in non_pks
|
||||
for f in non_pks_non_generated
|
||||
if f.name in update_fields or f.attname in update_fields
|
||||
]
|
||||
|
||||
@ -1030,7 +1034,7 @@ class Model(AltersData, metaclass=ModelBase):
|
||||
None,
|
||||
(getattr(self, f.attname) if raw else f.pre_save(self, False)),
|
||||
)
|
||||
for f in non_pks
|
||||
for f in non_pks_non_generated
|
||||
]
|
||||
forced_update = update_fields or force_update
|
||||
updated = self._do_update(
|
||||
|
@ -9,4 +9,6 @@ Django 5.0.5 fixes several bugs in 5.0.4.
|
||||
Bugfixes
|
||||
========
|
||||
|
||||
* ...
|
||||
* Fixed a bug in Django 5.0 that caused a crash of ``Model.save()`` when
|
||||
creating an instance of a model with a ``GeneratedField`` and providing a
|
||||
primary key (:ticket:`35350`).
|
||||
|
@ -207,6 +207,12 @@ class GeneratedFieldTestMixin:
|
||||
m.refresh_from_db()
|
||||
self.assertEqual(m.field, 8)
|
||||
|
||||
def test_save_model_with_pk(self):
|
||||
m = self.base_model(pk=1, a=1, b=2)
|
||||
m.save()
|
||||
m = self._refresh_if_needed(m)
|
||||
self.assertEqual(m.field, 3)
|
||||
|
||||
def test_save_model_with_foreign_key(self):
|
||||
fk_object = Foo.objects.create(a="abc", d=Decimal("12.34"))
|
||||
m = self.base_model(a=1, b=2, fk=fk_object)
|
||||
|
Loading…
x
Reference in New Issue
Block a user