[Django] #34933: update_or_create() does not persist field changes in save() when performing an update

8 views
Skip to first unread message

Django

unread,
Oct 27, 2023, 11:26:23 AM10/27/23
to django-...@googlegroups.com
#34933: update_or_create() does not persist field changes in save() when performing
an update
-------------------------------------+-------------------------------------
Reporter: cbasah | Owner: nobody
Type: Bug | Status: new
Component: Database | Version: 4.2
layer (models, ORM) |
Severity: Normal | Keywords: update_or_create
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
It seems when ''update_or_create()'' is updating an object, field updates
set in the model's overriden ''save()'' method is not persisted in the
database.

The following simple model illustrates this behavior. ''field1'' is set
via the ''default={...}'' argument while ''field2'' is an increment of
''field1'' by 1 performed in the model's overridden ''save()'' method.

{{{#!python
class Test(models.Model):
field1 = models.PositiveSmallIntegerField(blank=True, null=True)
field2 = models.PositiveSmallIntegerField(blank=True, null=True)

def save(self, *args, **kwargs):
self.field2 = self.field1 + 1
super().save(*args, **kwargs)
}}}

The following call for ''update_or_create()'' '''inserts''' the object
with id 1
{{{#!python
>>> (obj, is_created) = Test.objects.update_or_create(id=1,
defaults={'field1': 1})
>>> Test.objects.get(id=1).field1
1
>>> Test.objects.get(id=1).field2
2
}}}

The following call '''updates''' the same object
{{{#!python
>>> (obj, is_created) = Test.objects.update_or_create(id=1,
defaults={'field1': 10})
>>> Test.objects.get(id=1).field1
10
>>> Test.objects.get(id=1).field2
2
}}}
However, field2 does not get updated to 11.

--
Ticket URL: <https://code.djangoproject.com/ticket/34933>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Oct 27, 2023, 11:38:35 AM10/27/23
to django-...@googlegroups.com
#34933: update_or_create() does not persist field changes in save() when performing
an update
-------------------------------------+-------------------------------------
Reporter: Crispin Ali Basah | Owner: nobody
Type: Bug | Status: closed
Component: Database layer | Version: 4.2
(models, ORM) |
Severity: Normal | Resolution: duplicate
Keywords: update_or_create | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Crispin Ali Basah):

* status: new => closed
* resolution: => duplicate


--
Ticket URL: <https://code.djangoproject.com/ticket/34933#comment:1>

Django

unread,
Oct 27, 2023, 11:47:09 AM10/27/23
to django-...@googlegroups.com
#34933: update_or_create() does not persist field changes in save() when performing
an update
-------------------------------------+-------------------------------------
Reporter: Crispin Ali Basah | Owner: nobody
Type: Bug | Status: closed
Component: Database layer | Version: 4.2
(models, ORM) |
Severity: Normal | Resolution: duplicate
Keywords: update_or_create | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Crispin Ali Basah):

Duplicate of ticket:34099

--
Ticket URL: <https://code.djangoproject.com/ticket/34933#comment:2>

Reply all
Reply to author
Forward
0 new messages