设置.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'diva',
'USER': 'root',
'PASSWORD': 'admin',
'ATOMIC_REQUESTS':True,
'HOST': 'localhost',
'PORT': '3306',
},
Run Code Online (Sandbox Code Playgroud)
}
视图.py
def create_project(self, request):
try:
with transaction.atomic():
code here
except Exception as e:
print "Exception--->",str(e)
response = {"status":"failed",'response': ugettext("projects.services.create_project.failure")}
stat = status.HTTP_400_BAD_REQUEST
return Response(response, status=stat)
Run Code Online (Sandbox Code Playgroud)
在我的代码中,如果它引发 ObjectDoesNotExist 异常 回滚没有发生,任何人都可以用示例解释事务在 django 中的工作原理。