Django 事务 ATOMIC_REQUESTS
Django Transactions ATOMIC_REQUESTS
我真的不清楚 Django 中是如何设置原子请求的。当数据库设置中 ATOMIC_REQUESTS 设置为 True 时,这是否意味着事务中的所有视图现在 运行?如果我只希望某些视图在交易中 运行 怎么办?然后我是否需要在带有 @transaction.non_atomic_requests
装饰器的交易中明确定义所有其他不是 运行 的东西?
当在数据库设置中将 ATOMIC_REQUESTS
设置为 True
是否意味着事务中的所有视图现在 运行?
是的。来自 docs:
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
装饰器的交易中明确定义所有其他不是 运行 的东西?
是的。
When ATOMIC_REQUESTS
is enabled, it’s still possible to prevent views from running in a transaction. [The non_atomic_requests
] decorator will negate the effect of ATOMIC_REQUESTS
for a given view.
不过,一旦你要根据具体情况决定应该使用交易的地方,我宁愿不使用 ATOMIC_REQUESTS
而只使用 transaction.atomic
(无论作为装饰器或上下文管理器)在适当的地方。这是来自 the documentation 的示例:
@transaction.atomic
def viewfunc(request):
# This code executes inside a transaction.
do_stuff()
我真的不清楚 Django 中是如何设置原子请求的。当数据库设置中 ATOMIC_REQUESTS 设置为 True 时,这是否意味着事务中的所有视图现在 运行?如果我只希望某些视图在交易中 运行 怎么办?然后我是否需要在带有 @transaction.non_atomic_requests
装饰器的交易中明确定义所有其他不是 运行 的东西?
当在数据库设置中将 ATOMIC_REQUESTS
设置为 True
是否意味着事务中的所有视图现在 运行?
是的。来自 docs:
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
装饰器的交易中明确定义所有其他不是 运行 的东西?
是的。
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
(无论作为装饰器或上下文管理器)在适当的地方。这是来自 the documentation 的示例:
@transaction.atomic
def viewfunc(request):
# This code executes inside a transaction.
do_stuff()