小编Ram*_*tta的帖子

Django - 引发异常时不会发生回滚

设置.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 中的工作原理。

python django transactions atomic django-database

3
推荐指数
1
解决办法
1177
查看次数

标签 统计

atomic ×1

django ×1

django-database ×1

python ×1

transactions ×1