Atomic operations in Django?

后端 未结 7 1752
陌清茗
陌清茗 2020-12-13 03:21

I\'m trying to implement (what I think is) a pretty simple data model for a counter:

class VisitorDayTypeCounter(models.Model):
    visitType = models.CharFi         


        
7条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-13 03:28

    Your should use database transactions to avoid this kind of race condition. A transaction lets you perform the whole operation of creating, reading, incrementing and saving the counter on an "all or nothing" base. If anything goes wrong it will roll back the whole thing and you can try again.

    Check out the Django docs. There is a transaction middle ware, or you can use decorators around views or methods to create transactions.

提交回复
热议问题