mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-08-02 11:30:12 +03:00
Adding transaction rollback to exception_handler
This commit is contained in:
parent
6ceb0fa94a
commit
13ec64e8c8
|
@ -7,6 +7,8 @@ from django.core.exceptions import PermissionDenied
|
||||||
from django.http import Http404
|
from django.http import Http404
|
||||||
from django.utils.datastructures import SortedDict
|
from django.utils.datastructures import SortedDict
|
||||||
from django.views.decorators.csrf import csrf_exempt
|
from django.views.decorators.csrf import csrf_exempt
|
||||||
|
from django.db import transaction
|
||||||
|
|
||||||
from rest_framework import status, exceptions
|
from rest_framework import status, exceptions
|
||||||
from rest_framework.compat import smart_text, HttpResponseBase, View
|
from rest_framework.compat import smart_text, HttpResponseBase, View
|
||||||
from rest_framework.request import Request
|
from rest_framework.request import Request
|
||||||
|
@ -366,6 +368,14 @@ class APIView(View):
|
||||||
if response is None:
|
if response is None:
|
||||||
raise
|
raise
|
||||||
|
|
||||||
|
# We've suppressed the exception but still need to rollback any transaction.
|
||||||
|
if hasattr(transaction, 'set_rollback'):
|
||||||
|
# If running in >=1.6 then mark a rollback as required and let Django deal with it.
|
||||||
|
transaction.set_rollback(True) # Django 1.6+
|
||||||
|
elif transaction.is_managed():
|
||||||
|
# If running <=1.5 and TransactionMiddleware is installed then force it's rollback behavior.
|
||||||
|
TransactionMiddleware().process_exception(request, None)
|
||||||
|
|
||||||
response.exception = True
|
response.exception = True
|
||||||
return response
|
return response
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user