Django中的TransactionManagementError

时间:2018-01-22 15:12:47

标签: python django

我在Django中创建了一个自定义用户模型。我现在想用另一个名为Ambassadors的模型来扩展这个模型。

我得到的错误是“TransactionManagementError”。无论我尝试或阅读什么,我都找不到问题。你知道问题出在哪里吗?附上了Traceback以及models.py

Environment:


Request Method: POST
Request URL: http://127.0.0.1:8000/admin/accounts/myuser/add/

Django Version: 2.0.1
Python Version: 3.6.4
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'accounts']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']



Traceback:

    File "/Users/Marc/Desktop/Dev/accounts/lib/python3.6/site-packages/django/core/handlers/exception.py" in inner
      35.             response = get_response(request)

    File "/Users/Marc/Desktop/Dev/accounts/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
      128.                 response = self.process_exception_by_middleware(e, request)

    File "/Users/Marc/Desktop/Dev/accounts/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
      126.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

    File "/Users/Marc/Desktop/Dev/accounts/lib/python3.6/site-packages/django/contrib/admin/options.py" in wrapper
      574.                 return self.admin_site.admin_view(view)(*args, **kwargs)

    File "/Users/Marc/Desktop/Dev/accounts/lib/python3.6/site-packages/django/utils/decorators.py" in _wrapped_view
      142.                     response = view_func(request, *args, **kwargs)

    File "/Users/Marc/Desktop/Dev/accounts/lib/python3.6/site-packages/django/views/decorators/cache.py" in _wrapped_view_func
      44.         response = view_func(request, *args, **kwargs)

    File "/Users/Marc/Desktop/Dev/accounts/lib/python3.6/site-packages/django/contrib/admin/sites.py" in inner
      223.             return view(request, *args, **kwargs)

    File "/Users/Marc/Desktop/Dev/accounts/lib/python3.6/site-packages/django/utils/decorators.py" in _wrapper
      62.             return bound_func(*args, **kwargs)

    File "/Users/Marc/Desktop/Dev/accounts/lib/python3.6/site-packages/django/views/decorators/debug.py" in sensitive_post_parameters_wrapper
      76.             return view(request, *args, **kwargs)

    File "/Users/Marc/Desktop/Dev/accounts/lib/python3.6/site-packages/django/utils/decorators.py" in bound_func
      58.                 return func.__get__(self, type(self))(*args2, **kwargs2)

    File "/Users/Marc/Desktop/Dev/accounts/lib/python3.6/site-packages/django/utils/decorators.py" in _wrapper
      62.             return bound_func(*args, **kwargs)

    File "/Users/Marc/Desktop/Dev/accounts/lib/python3.6/site-packages/django/utils/decorators.py" in _wrapped_view
      142.                     response = view_func(request, *args, **kwargs)

    File "/Users/Marc/Desktop/Dev/accounts/lib/python3.6/site-packages/django/utils/decorators.py" in bound_func
      58.                 return func.__get__(self, type(self))(*args2, **kwargs2)

    File "/Users/Marc/Desktop/Dev/accounts/lib/python3.6/site-packages/django/contrib/auth/admin.py" in add_view
      100.             return self._add_view(request, form_url, extra_context)

    File "/Users/Marc/Desktop/Dev/accounts/lib/python3.6/site-packages/django/contrib/auth/admin.py" in _add_view
      127.         return super().add_view(request, form_url, extra_context)

    File "/Users/Marc/Desktop/Dev/accounts/lib/python3.6/site-packages/django/contrib/admin/options.py" in add_view
      1553.         return self.changeform_view(request, None, form_url, extra_context)

    File "/Users/Marc/Desktop/Dev/accounts/lib/python3.6/site-packages/django/utils/decorators.py" in _wrapper
      62.             return bound_func(*args, **kwargs)

    File "/Users/Marc/Desktop/Dev/accounts/lib/python3.6/site-packages/django/utils/decorators.py" in _wrapped_view
      142.                     response = view_func(request, *args, **kwargs)

    File "/Users/Marc/Desktop/Dev/accounts/lib/python3.6/site-packages/django/utils/decorators.py" in bound_func
      58.                 return func.__get__(self, type(self))(*args2, **kwargs2)

    File "/Users/Marc/Desktop/Dev/accounts/lib/python3.6/site-packages/django/contrib/admin/options.py" in changeform_view
      1450.             return self._changeform_view(request, object_id, form_url, extra_context)

    File "/Users/Marc/Desktop/Dev/accounts/lib/python3.6/site-packages/django/contrib/admin/options.py" in _changeform_view
      1494.                     self.log_addition(request, new_object, change_message)

    File "/Users/Marc/Desktop/Dev/accounts/lib/python3.6/site-packages/django/contrib/admin/options.py" in log_addition
      773.             change_message=message,

    File "/Users/Marc/Desktop/Dev/accounts/lib/python3.6/site-packages/django/contrib/admin/models.py" in log_action
      29.             change_message=change_message,

    File "/Users/Marc/Desktop/Dev/accounts/lib/python3.6/site-packages/django/db/models/manager.py" in manager_method
      82.                 return getattr(self.get_queryset(), name)(*args, **kwargs)

    File "/Users/Marc/Desktop/Dev/accounts/lib/python3.6/site-packages/django/db/models/query.py" in create
      417.         obj.save(force_insert=True, using=self.db)

    File "/Users/Marc/Desktop/Dev/accounts/lib/python3.6/site-packages/django/db/models/base.py" in save
      729.                        force_update=force_update, update_fields=update_fields)

    File "/Users/Marc/Desktop/Dev/accounts/lib/python3.6/site-packages/django/db/models/base.py" in save_base
      759.             updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)

    File "/Users/Marc/Desktop/Dev/accounts/lib/python3.6/site-packages/django/db/models/base.py" in _save_table
      842.             result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)

    File "/Users/Marc/Desktop/Dev/accounts/lib/python3.6/site-packages/django/db/models/base.py" in _do_insert
      880.                                using=using, raw=raw)

    File "/Users/Marc/Desktop/Dev/accounts/lib/python3.6/site-packages/django/db/models/manager.py" in manager_method
      82.                 return getattr(self.get_queryset(), name)(*args, **kwargs)

    File "/Users/Marc/Desktop/Dev/accounts/lib/python3.6/site-packages/django/db/models/query.py" in _insert
      1125.         return query.get_compiler(using=using).execute_sql(return_id)

    File "/Users/Marc/Desktop/Dev/accounts/lib/python3.6/site-packages/django/db/models/sql/compiler.py" in execute_sql
      1281.                 cursor.execute(sql, params)

    File "/Users/Marc/Desktop/Dev/accounts/lib/python3.6/site-packages/django/db/backends/utils.py" in execute
      100.             return super().execute(sql, params)

    File "/Users/Marc/Desktop/Dev/accounts/lib/python3.6/site-packages/django/db/backends/utils.py" in execute
      68.         return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)

    File "/Users/Marc/Desktop/Dev/accounts/lib/python3.6/site-packages/django/db/backends/utils.py" in _execute_with_wrappers
      77.         return executor(sql, params, many, context)

    File "/Users/Marc/Desktop/Dev/accounts/lib/python3.6/site-packages/django/db/backends/utils.py" in _execute
      80.         self.db.validate_no_broken_transaction()

    File "/Users/Marc/Desktop/Dev/accounts/lib/python3.6/site-packages/django/db/backends/base/base.py" in validate_no_broken_transaction
      437.                 "An error occurred in the current transaction. You can't "

    Exception Type: TransactionManagementError at /admin/accounts/myuser/add/
    Exception Value: An error occurred in the current transaction. You can't execute queries until the end of the 'atomic' block.

models.py

from django.conf import settings
from django.contrib.auth.models import (
    BaseUserManager, AbstractBaseUser
)
from django.db import models
from django.db.models.signals import post_save

# Create your models here.
class MyUserManager(BaseUserManager):
    def create_user(self, email, password=None):
        """
        Creates and saves a User with the given email, date of
        birth and password.
        """
        if not email:
            raise ValueError('Users must have an email address')

        user = self.model(
            email=self.normalize_email(email),
        )

        user.set_password(password)
        user.save(using=self._db)
        return user

    def create_superuser(self, email, password):
        """
        Creates and saves a superuser with the given email, date of
        birth and password.
        """
        user = self.create_user(
            email,
            password=password,
        )
        user.is_admin = True
        user.save(using=self._db)
        return user


class MyUser(AbstractBaseUser):
    email = models.EmailField(
        verbose_name='email address',
        max_length=255,
        unique=True,
    )
    is_active = models.BooleanField(default=True)
    is_admin = models.BooleanField(default=False)

    objects = MyUserManager()

    USERNAME_FIELD = 'email'
    #REQUIRED_FIELDS = ['']

    def __str__(self):
        return self.email

    def has_perm(self, perm, obj=None):
        "Does the user have a specific permission?"
        # Simplest possible answer: Yes, always
        return True

    def has_module_perms(self, app_label):
        "Does the user have permissions to view the app `app_label`?"
        # Simplest possible answer: Yes, always
        return True

    @property
    def is_staff(self):
        "Is the user a member of staff?"
        # Simplest possible answer: All admins are staff
        return self.is_admin


class Ambassador(models.Model):
    user = models.ForeignKey(
        settings.AUTH_USER_MODEL,
        on_delete=models.CASCADE,
    )
    coupon_id = models.IntegerField()
    coupon_code = models.CharField(max_length=30)

    def __str__(self):
        return str(self.user.email)

def post_save_receiver(sender, instance, created, **kwargs):
    if created:
        try:
            Ambassador.objects.create(user=instance)
        except:
            pass

post_save.connect(post_save_receiver, sender=settings.AUTH_USER_MODEL)

2 个答案:

答案 0 :(得分:0)

所以我在寻找解决方案方面迈出了一大步。将coupon_id = models.IntegerField()更改为coupon_id = models.IntegerField(blank=True, null=True)后,它突然变得有效了。我不明白100%的原因,但也许这里有人可以向我解释一下吗?

答案 1 :(得分:0)

你有一个创建大使的保存后信号。

def post_save_receiver(sender, instance, created, **kwargs):
    if created:
        try:
            Ambassador.objects.create(user=instance)
        except:
            pass

但是,您尚未指定coupon_id并且它已null=False,因此提出了IntegrityError。由于异常没有被原子块捕获,因此你得到了模糊的TransactionManagementError

您可以在创建对象时指定coupon_id,向字段添加默认值或将字段更改为null=True来解决问题。

一个关键的教训是,执行try: except来捕获所有异常通常是一个坏主意。在这种情况下,代码永远不会起作用,但是因为你执行了pass代码并且你可能没有发现它,除非它导致了第二个错误。