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.
* status: new => closed
* resolution: => duplicate
--
Ticket URL: <https://code.djangoproject.com/ticket/34933#comment:1>
Comment (by Crispin Ali Basah):
Duplicate of ticket:34099
--
Ticket URL: <https://code.djangoproject.com/ticket/34933#comment:2>