django admin 当自动提交关闭时,最外层的 'atomic' 块不能使用 savepoint = False

django admin The outermost 'atomic' block cannot use savepoint = False when autocommit is off

当我尝试从 django admin 生成的 table 中删除一个项目时,它会抛出这个错误

Environment:


Request Method: POST
Request URL: http://127.0.0.1:8000/admin/sybase_app/packageweight/?q=493

Django Version: 1.8
Python Version: 3.6.9
Installed Applications:
('django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'blog',
 'sybase_app')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware',
 'django.middleware.security.SecurityMiddleware')


Traceback:
File "/home/pd/.local/lib/python3.6/site-packages/django/core/handlers/base.py" in get_response
  132.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/pd/.local/lib/python3.6/site-packages/django/contrib/admin/options.py" in wrapper
  616.                 return self.admin_site.admin_view(view)(*args, **kwargs)
File "/home/pd/.local/lib/python3.6/site-packages/django/utils/decorators.py" in _wrapped_view
  110.                     response = view_func(request, *args, **kwargs)
File "/home/pd/.local/lib/python3.6/site-packages/django/views/decorators/cache.py" in _wrapped_view_func
  57.         response = view_func(request, *args, **kwargs)
File "/home/pd/.local/lib/python3.6/site-packages/django/contrib/admin/sites.py" in inner
  233.             return view(request, *args, **kwargs)
File "/home/pd/.local/lib/python3.6/site-packages/django/utils/decorators.py" in _wrapper
  34.             return bound_func(*args, **kwargs)
File "/home/pd/.local/lib/python3.6/site-packages/django/utils/decorators.py" in _wrapped_view
  110.                     response = view_func(request, *args, **kwargs)
File "/home/pd/.local/lib/python3.6/site-packages/django/utils/decorators.py" in bound_func
  30.                 return func.__get__(self, type(self))(*args2, **kwargs2)
File "/home/pd/.local/lib/python3.6/site-packages/django/contrib/admin/options.py" in changelist_view
  1590.                 response = self.response_action(request, queryset=cl.get_queryset(request))
File "/home/pd/.local/lib/python3.6/site-packages/django/contrib/admin/options.py" in response_action
  1333.             response = func(self, request, queryset)
File "/home/pd/.local/lib/python3.6/site-packages/django/contrib/admin/actions.py" in delete_selected
  49.             queryset.delete()
File "/home/pd/.local/lib/python3.6/site-packages/django/db/models/query.py" in delete
  537.         collector.delete()
File "/home/pd/.local/lib/python3.6/site-packages/django/db/models/deletion.py" in delete
  282.         with transaction.atomic(using=self.using, savepoint=False):
File "/home/pd/.local/lib/python3.6/site-packages/django/db/transaction.py" in __enter__
  164.                         "The outermost 'atomic' block cannot use "

Exception Type: TransactionManagementError at /admin/sybase_app/packageweight/
Exception Value: The outermost 'atomic' block cannot use savepoint = False when autocommit is off.

我该如何解决?

该限制已在 Django 1.8.5 中删除(提交:django/django@425c5e4)。

但是,Delete selected 功能在自动提交关闭时仍然无法使用。

1。如果意外关闭,恢复自动提交

如果 settings.py 中有 'AUTOCOMMIT': False,请将其删除,该功能应该可以使用。

DATABASES = {
    'default': {
        ...
        'AUTOCOMMIT': False,  # Remove this line
    }
}

2。用 transaction.atomic

修补动作

如果您需要关闭自动提交,请在 manage.py 或 AppConfig.

中修补 actions.delete_selected
from django.contrib.admin import actions
from django.db import transaction
actions.delete_selected = transaction.atomic(actions.delete_selected)