diff --git a/rest_framework/compat.py b/rest_framework/compat.py index c9efbfbf3..5009ffee1 100644 --- a/rest_framework/compat.py +++ b/rest_framework/compat.py @@ -12,7 +12,7 @@ from django.apps import apps from django.conf import settings from django.core import validators from django.core.exceptions import ImproperlyConfigured -from django.db import connection, models, transaction +from django.db import models from django.utils import six from django.views.generic import View @@ -297,12 +297,6 @@ class MaxLengthValidator(CustomValidatorMessage, validators.MaxLengthValidator): pass -def set_rollback(): - if connection.settings_dict.get('ATOMIC_REQUESTS', False): - if connection.in_atomic_block: - transaction.set_rollback(True) - - def authenticate(request=None, **credentials): from django.contrib.auth import authenticate if django.VERSION < (1, 11): diff --git a/rest_framework/views.py b/rest_framework/views.py index dfed15888..3140bb9a3 100644 --- a/rest_framework/views.py +++ b/rest_framework/views.py @@ -5,7 +5,7 @@ from __future__ import unicode_literals from django.conf import settings from django.core.exceptions import PermissionDenied -from django.db import models +from django.db import connection, models, transaction from django.http import Http404 from django.http.response import HttpResponseBase from django.utils import six @@ -16,7 +16,6 @@ from django.views.decorators.csrf import csrf_exempt from django.views.generic import View from rest_framework import exceptions, status -from rest_framework.compat import set_rollback from rest_framework.request import Request from rest_framework.response import Response from rest_framework.schemas import AutoSchema @@ -55,6 +54,12 @@ def get_view_description(view_cls, html=False): return description +def set_rollback(): + atomic_requests = connection.settings_dict.get('ATOMIC_REQUESTS', False) + if atomic_requests and connection.in_atomic_block: + transaction.set_rollback(True) + + def exception_handler(exc, context): """ Returns the response that should be used for any given exception.