mirror of
https://github.com/django/django.git
synced 2025-01-22 00:02:15 +00:00
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.
This commit is contained in:
parent
ca5cd3e3e8
commit
8b53560eea
@ -1063,12 +1063,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
|
||||
]
|
||||
|
||||
@ -1100,7 +1104,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