擅长:python、mysql、java
<p>对我来说这在Django 2.2.5工作</p>
<p>首先在你的设置中.py</p>
<pre><code>...
DATABASES = {
'default': {
'ENGINE': 'xxx', # transactional db
...
'ATOMIC_REQUESTS': True,
}
}
</code></pre>
<p>在你的函数中(views.py)</p>
<pre><code>from django.db import transaction
@transaction.atomic
def make_db_stuff():
# do stuff in your db (inserts or whatever)
if success:
return True
else:
transaction.set_rollback(True)
return False
</code></pre>