将我的项目从django 1.5.1升级到1.6.5时,我面临着这个奇怪的问题。
这是一个原子'这是禁止的。阻止活跃。
我知道Database Transaction changes for django 1.6并相应地更改了设置。除了访问request.user
对象外,大部分都可以使用。
代码,例如:
with transaction.atomic():
if hasattr(request, 'user') and getattr(request.user, 'id', None):
#blah
这是stacktrace:
Environment:
Request Method: GET
Request URL: <domain>/api/v1/browser_id/
Django Version: 1.6.5
Python Version: 2.7.3
<Installed Applications & Middlewares snipped for brevity>
Traceback:
File "/usr/local/virtualenvs/karthik-django165/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
112. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/usr/local/virtualenvs/karthik-django165/local/lib/python2.7/site-packages/django/db/transaction.py" in inner
371. return func(*args, **kwargs)
File "/home/kravindra/workspace/puppysite/puppy/kennel/views/etag_session.py" in browser_id
43. if hasattr(request, 'user') and getattr(request.user, 'id', None):
File "/usr/local/virtualenvs/karthik-django165/local/lib/python2.7/site-packages/django/utils/functional.py" in inner
213. self._setup()
File "/usr/local/virtualenvs/karthik-django165/local/lib/python2.7/site-packages/django/utils/functional.py" in _setup
298. self._wrapped = self._setupfunc()
File "/usr/local/virtualenvs/karthik-django165/local/lib/python2.7/site-packages/django/contrib/auth/middleware.py" in <lambda>
18. request.user = SimpleLazyObject(lambda: get_user(request))
File "/usr/local/virtualenvs/karthik-django165/local/lib/python2.7/site-packages/django/contrib/auth/middleware.py" in get_user
10. request._cached_user = auth.get_user(request)
File "/usr/local/virtualenvs/karthik-django165/local/lib/python2.7/site-packages/django/contrib/auth/__init__.py" in get_user
140. user_id = request.session[SESSION_KEY]
File "/usr/local/virtualenvs/karthik-django165/local/lib/python2.7/site-packages/django/contrib/sessions/backends/base.py" in __getitem__
47. return self._session[key]
File "/usr/local/virtualenvs/karthik-django165/local/lib/python2.7/site-packages/django/contrib/sessions/backends/base.py" in _get_session
173. self._session_cache = self.load()
File "/usr/local/virtualenvs/karthik-django165/local/lib/python2.7/site-packages/django/contrib/sessions/backends/cached_db.py" in load
52. self.create()
File "/usr/local/virtualenvs/karthik-django165/local/lib/python2.7/site-packages/django/contrib/sessions/backends/db.py" in create
40. self.save(must_create=True)
File "/usr/local/virtualenvs/karthik-django165/local/lib/python2.7/site-packages/django/contrib/sessions/backends/cached_db.py" in save
62. super(SessionStore, self).save(must_create)
File "/usr/local/virtualenvs/karthik-django165/local/lib/python2.7/site-packages/django/contrib/sessions/backends/db.py" in save
63. obj.save(force_insert=must_create, using=using)
File "/usr/local/virtualenvs/karthik-django165/local/lib/python2.7/site-packages/django/db/models/base.py" in save
545. force_update=force_update, update_fields=update_fields)
File "/usr/local/virtualenvs/karthik-django165/local/lib/python2.7/site-packages/django/db/models/base.py" in save_base
582. update_fields=update_fields, raw=raw, using=using)
File "/usr/local/virtualenvs/karthik-django165/local/lib/python2.7/site-packages/django/dispatch/dispatcher.py" in send
185. response = receiver(signal=self, sender=sender, **named)
File "/usr/local/virtualenvs/karthik-django165/local/lib/python2.7/site-packages/django/db/transaction.py" in inner
430. with self:
File "/usr/local/virtualenvs/karthik-django165/local/lib/python2.7/site-packages/django/db/transaction.py" in __enter__
422. self.entering(self.using)
File "/usr/local/virtualenvs/karthik-django165/local/lib/python2.7/site-packages/django/db/transaction.py" in entering
483. enter_transaction_management(using=using)
File "/usr/local/virtualenvs/karthik-django165/local/lib/python2.7/site-packages/django/db/transaction.py" in enter_transaction_management
70. get_connection(using).enter_transaction_management(managed, forced)
File "/usr/local/virtualenvs/karthik-django165/local/lib/python2.7/site-packages/django/db/backends/__init__.py" in enter_transaction_management
287. self.validate_no_atomic_block()
File "/usr/local/virtualenvs/karthik-django165/local/lib/python2.7/site-packages/django/db/backends/__init__.py" in validate_no_atomic_block
367. "This is forbidden when an 'atomic' block is active.")
Exception Type: TransactionManagementError at /api/v1/browser_id/
Exception Value: This is forbidden when an 'atomic' block is active.
查看堆栈跟踪,
/usr/local/virtualenvs/karthik-django165/local/lib/python2.7/site-packages/django/contrib/sessions/backends/cached_db.py in load
self.create() ...
▼ Local vars
Variable Value
e
DoesNotExist('Session matching query does not exist.',)
self
<django.contrib.sessions.backends.cached_db.SessionStore object at 0x7f61d401c6d0>
data
None
SessionStore
引发了异常。
使用POSTGRES作为后端数据库。 后端会话存储相关设置为:
SESSION_ENGINE = 'django.contrib.sessions.backends.cached_db' #which is persistent storage
ATOMIC_REQUESTS = True
有关如何解决此问题的任何指示?
我发现有很多关于SO的问题,以及一般解决TransactionManagementError
问题的博客帖子,但没有特别解决这个问题
答案 0 :(得分:6)
从回溯看来,您的post_save
信号接收器之一仍在使用以下不推荐使用的transaction
API之一:
transaction.Transaction
transaction.autocommit
transaction.commit_on_success
transaction.commit_manually
commit_on_success_unless_managed
请确保将其替换为transaction.atomic
实例,并且您的问题应该消失。
如果您在找到罪魁祸首接收器时遇到问题,请告诉我。