For example, I have model:
class CacheMetaData(models.Model):
count = models.IntegerField(default=0)
def inc(self):
self.count += 1
self.save()
If I define method inc()
like above, then Django will execute a SQL statement like:
UPDATE the_table set count = 1 where id = 1
This way lead to race condition so that count
might not be correctly increased. I want Django to execute:
update the_table set count = count + 1
So that count will always be correctly increased.
Can Django do that? If yes, how to define the field count
?