Django交易记录ATOMIC_REQUESTS [英] Django Transactions ATOMIC_REQUESTS
问题描述
我真的不清楚在Django中如何设置原子请求.在数据库设置中将ATOMIC_REQUESTS设置为True时,是否意味着现在所有视图都在事务中运行?如果我只希望只在事务中运行某些视图怎么办?然后,是否需要使用 @ transaction.non_atomic_requests
装饰器显式定义未在事务中运行的所有其他对象?
I'm really unclear how atomic requests are set in Django. When ATOMIC_REQUESTS is set to True in the DB settings does that mean that all views now run in a transaction? What if I want only want only certain views to be run in a transaction? Do I then need to explicitly define all the others that are not run in a transaction with a @transaction.non_atomic_requests
decorator?
推荐答案
在数据库设置中将 ATOMIC_REQUESTS
设置为 True
时,这意味着现在所有视图都在事务中运行?
When ATOMIC_REQUESTS
is set to True
in the DB settings does that mean that all views now run in a transaction?
是的.从 docs :
在调用视图函数之前,Django启动一个事务.如果产生的响应没有问题,则Django提交事务.如果视图产生异常,则Django将回滚事务.
Before calling a view function, Django starts a transaction. If the response is produced without problems, Django commits the transaction. If the view produces an exception, Django rolls back the transaction.
然后是否需要使用 @ transaction.non_atomic_requests
装饰器来明确定义未在事务中运行的所有其他对象?
Do I then need to explicitly define all the others that are not run in a transaction with a @transaction.non_atomic_requests
decorator?
是的
启用
ATOMIC_REQUESTS
后,仍然可以阻止视图在事务中运行.[non_atomic_requests
]装饰器将取消给定视图的ATOMIC_REQUESTS
的效果.
When
ATOMIC_REQUESTS
is enabled, it’s still possible to prevent views from running in a transaction. [Thenon_atomic_requests
] decorator will negate the effect ofATOMIC_REQUESTS
for a given view.
不过,一旦决定要根据具体情况使用事务,我宁愿不使用 ATOMIC_REQUESTS
而只使用 transaction.atomic
(无论是装饰器还是上下文管理器).这是来自文档:
Once you're at the point of deciding on a case-by-case basis where transactions should be used, though, I prefer to not use ATOMIC_REQUESTS
and just use transaction.atomic
(whether as a decorator or a context manager) where appropriate. Here's an example from the documentation:
@transaction.atomic
def viewfunc(request):
# This code executes inside a transaction.
do_stuff()
这篇关于Django交易记录ATOMIC_REQUESTS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!