事务模块对象没有属性原子

时间:2013-12-09 23:54:16

标签: django django-models

我无法弄清楚这一点。我不断得到一个'module'对象在事务对象上没有属性'atomic'。我不知道为什么这会自发地破裂。我记不住了,但我可能已经更新了django的服务器版本。

from django.db import transaction

@receiver(post_save, sender=User)
def saveUserAndInfo(sender, instance, **kwargs):
    user = instance
    try:
        with transaction.atomic():
            user.user_info.save()
    except UserInfo.DoesNotExist:
        info = UserInfo()
        info.user = user
        info.save()

回溯:

File "/home2/univetg1/python27/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  115.                         response = callback(request, *callback_args, **callback_kwargs)
File "/home2/univetg1/lunchbox/userinfo/views.py" in facebookLogin
  190.                 login(request, user)
File "/home2/univetg1/python27/lib/python2.7/site-packages/django/contrib/auth/__init__.py" in login
  95.     user_logged_in.send(sender=user.__class__, request=request, user=user)
File "/home2/univetg1/python27/lib/python2.7/site-packages/django/dispatch/dispatcher.py" in send
  170.             response = receiver(signal=self, sender=sender, **named)
File "/home2/univetg1/python27/lib/python2.7/site-packages/django/contrib/auth/models.py" in update_last_login
  31.     user.save(update_fields=['last_login'])
File "/home2/univetg1/python27/lib/python2.7/site-packages/django/db/models/base.py" in save
  546.                        force_update=force_update, update_fields=update_fields)
File "/home2/univetg1/python27/lib/python2.7/site-packages/django/db/models/base.py" in save_base
  664.                                    update_fields=update_fields, raw=raw, using=using)
File "/home2/univetg1/python27/lib/python2.7/site-packages/django/dispatch/dispatcher.py" in send
  170.             response = receiver(signal=self, sender=sender, **named)
File "/home2/univetg1/lunchbox/userinfo/models.py" in saveUserAndInfo
  86.         with transaction.atomic():

Exception Type: AttributeError at /facebookLogin
Exception Value: 'module' object has no attribute 'atomic'

1 个答案:

答案 0 :(得分:4)

transaction.atomic仅在Django 1.6及以后版本中可用: https://docs.djangoproject.com/en/1.6/topics/db/transactions/#controlling-transactions-explicitly

您可能不小心将您的Django版本降级为1.5或更低