gpt4 book ai didi

python - 在 Django 中控制原子事务

转载 作者:行者123 更新时间:2023-11-29 05:12:12 26 4
gpt4 key购买 nike

我有一个简单的图书馆应用程序。为了强制将 3 个操作作为一个操作提交,并在任何操作失败时回滚,我进行了以下代码更改:

settings.py 中:

AUTOCOMMIT=False

forms.py

from django.db import IntegrityError, transaction
class CreateLoan(forms.Form):
#Fields...
def save(self):
id_book = form.cleaned_data.get('id_book', None)
id_customer = form.cleaned_data.get('id_customer', None)
start_date = form.cleaned_data.get('start_date', None)
book = Book.objects.get(id=id_book)
customer = Customer.objects.get(id=id_customer)
new_return = Return(
book=book
start_date=start_date)
txn=Loan_Txn(
customer=customer,
book=book,
start_date=start_date
)

try
with transaction.atomic():
book.update(status="ON_LOAN")
new_return.save(force_insert=True)
txn.save(force_insert=True)
except IntegrityError:
raise forms.ValidationError("Something occured. Please try again")

关于这个,我还遗漏了什么吗?我使用的是 Django 1.9 和 Python 3.4.3,数据库是 MySQL。

最佳答案

您正确地使用了 transaction.atomic()(包括将 try ... except 放在事务之外)但是您绝对不应该设置 自动提交 = 假

作为documentation状态,当你想“禁用 Django 的事务管理”时,你将系统范围的设置设置为 False——但这显然不是你想要做的,因为你是使用 transaction.atomic()!更多来自 documentation :

If you do this, Django won’t enable autocommit, and won’t perform any commits. You’ll get the regular behavior of the underlying database library. This requires you to commit explicitly every transaction, even those started by Django or by third-party libraries. Thus, this is best used in situations where you want to run your own transaction-controlling middleware or do something really strange.

所以不要那样做。 Django 当然会为该原子 block 禁用自动提交并在 block 完成时重新启用它。

关于python - 在 Django 中控制原子事务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37795302/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com