本文共 1527 字,大约阅读时间需要 5 分钟。
django查询数据库数据
()
( )
Django’s default behaviour is to run autocommit mode. Each query is immediately committed to the database unless a transaction is active.
Django的默认行为是运行自动提交模式。 除非事务处于活动状态,否则每个查询都会立即提交到数据库。
Django provides a single API to control database transactions. atomic allows us to create a block of code or a function within which the atomicity of the database is guaranteed. If the block of code is successfully completed, the changes are committed to the database. If there is an exception, the changes are rolled back.
Django提供了一个API来控制数据库事务。 atomic使我们可以创建代码块或函数,以保证数据库的原子性。 如果代码块成功完成,则更改将提交给数据库。 如果有异常,则更改将回滚。
atomic can be either used as a decorator or as a context manager.
原子可以用作装饰器或用作上下文管理器。
Here is an example for atomic as a decorator
这是atomic as a decorator
的示例
from django.db import transaction @transaction.atomicdef foo():do_more() # Multiple update queries executes inside a transaction
An example for atomic as a context manager
一个atomic as a context manager
的示例
from django.db import transaction def foo():do_stuff() # This code executes in Django's default autocommit mode. with transaction.atomic():do_more() # Multiple update queries executes inside a transaction
atomic
blocks can be nested. In this case, when an inner block completes successfully, its effects can still be rolled back if an exception is raised in the outer block at a later point.
atomic
块可以嵌套。 在这种情况下,当内部块成功完成时,如果稍后在外部块中引发异常,则仍然可以回滚其效果。
翻译自:
django查询数据库数据
转载地址:http://ezhwd.baihongyu.com/