#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.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/0107018b71be50dc-020fd46c-6eb7-4f0b-8641-5582969e075e-000000%40eu-central-1.amazonses.com.