开发者

objects.get_or_create() or transactions in Django views

开发者 https://www.devze.com 2023-02-08 22:53 出处:网络
OK, objects.get_or_create(), when called, will create a new record in the database (if there is no record I need). But what if the code throws an exception/fails AFTER objects.get_or_create() has been

OK, objects.get_or_create(), when called, will create a new record in the database (if there is no record I need). But what if the code throws an exception/fails AFTER objects.get_or_create() has been called?

Basically, I end up with a new record in the database which should not be there. To put it differently, shouldn't 开发者_StackOverflow社区the whole thing be wrapped in a transaction which is rolled back if there is a problem? Is it possible?


As Ignacio suggests the answer (in much greater detail than I would be capable of) is available in the django docs.

http://docs.djangoproject.com/en/dev/topics/db/transactions

0

精彩评论

暂无评论...
验证码 换一张
取 消