IntegrityError:唯一约束失败:usermanager_userprofile.email_address

时间:2019-09-05 11:22:13

标签: django django-models django-forms django-views django-2.2

我正在开发将用户模型与OneToOneField一起使用的注册应用。

models.py

from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.db import models
from django.dispatch import receiver

class UserProfile(models.Model):
    GENDER_CHOICES = (
        ('M', 'Male'),
        ('F', 'Female'),
    )

    user = models.OneToOneField(User, on_delete=models.CASCADE)
    name = models.CharField('First name', max_length=30)
    surname = models.CharField('Last name', max_length=150)
    gender = models.CharField('Gender', max_length=1, choices=GENDER_CHOICES)
    date_of_birth = models.DateField('Date of birth', null=True)
    profile_image = models.ImageField('Profile image', upload_to='users_upload/profiles/%Y/%m/%d')
    company_name = models.CharField('Company Name', max_length=75, blank=True, null=True)
    fiscal_data = models.CharField('Fiscal data', max_length=200)
    city = models.CharField('City', max_length=150)
    address = models.CharField('Address', max_length=200)
    postcode = models.CharField('Zip Code', max_length=50)
    country = models.CharField('Country', max_length=75)
    telephone_number = models.CharField('Telephone', max_length=20)
    email_address = models.EmailField('Email')

class Meta:
    unique_together = ['fiscal_data', 'email_address']


@receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
    if created:
        UserProfile.objects.create(user=instance)

@receiver(post_save, sender=User)
def save_user_profile(sender, instance, **kwargs):
    instance.userprofile.save()

forms.py

from django import forms
from django.contrib.auth import get_user_model, password_validation
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User

from .models import UserProfile

class UserForm(UserCreationForm):
    class Meta:
        model = User
        fields = ('username', 'password1', 'password2')

class UserProfileForm(forms.ModelForm):

    class Meta:
        model = UserProfile
        fields = [
            'name', 'surname', 'gender', 'date_of_birth',
            'user_type', 'profile_image', 'company_name',
            'address', 'postcode', 'city', 'country',
            'telephone_number', 'email_address',
            'fiscal_data',
            ]

views.py

from django.contrib.auth.forms import UserCreationForm
from django.db import transaction
from django.shortcuts import render

from .forms import UserProfileForm
from .models import UserProfile

@transaction.atomic
def createUser(request):
    if request.method == 'POST':
        user_form = UserCreationForm(request.POST)
        profile_form = UserProfileForm(request.POST or None, request.FILES or None)
        if user_form.is_valid() and profile_form.is_valid():
            user = user_form.save()
            user.refresh_from_db()  
            profile_form = UserProfileForm(request.POST, instance=user.userprofile) 
            profile_form.full_clean()  
            profile_form.save()  
    else:
        user_form = UserCreationForm()
        profile_form = UserProfileForm()
    return render(request, 'usermanager/editing/create_user.html', {
        'user_form': user_form,
        'profile_form': profile_form
    })

create_user.html

<form method="POST" enctype="multipart/form-data" novalidate>{% csrf_token %}

  <div class="card-body">
    {{ user_form.as_p }}
    {{ profile_form.as_p }}
  </div>

  <div class="card-footer">
    <div class="row justify-content-md-center">
      <div class="col-md-auto">
        <input type="submit" class="btn btn-danger shadow" value="Create Profile">
      </div>
    </div>
  </div>

</form>

当我尝试使用表单注册新用户时,我看到以下错误消息:

  

唯一约束失败:usermanager_userprofile.email_address

即使我尝试使用管理面板注册新用户,也会发生这种情况。

我该如何解决?

我已经使用this策略来创建模型,对于createUser视图,我已经使用了this

跟踪

  

环境:

     

请求方法:POST请求URL:http://127.0.0.1:8000/create-user/

     

Django版本:2.2.5 Python版本:3.6.8安装的应用程序:   ['django.contrib.admin','django.contrib.auth',   'django.contrib.contenttypes','django.contrib.sessions',   'django.contrib.messages','django.contrib.staticfiles','kernel',   'usermanager']已安装的中间件:   ['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']

     

跟踪:

     

文件   “ /home/max/Django/area-test/app_usermanager_dev/devenv/lib/python3.6/site-packages/django/core/handlers/exception.py”   在内部     34. response = get_response(request)

     

文件   “ /home/max/Django/area-test/app_usermanager_dev/devenv/lib/python3.6/site-packages/django/core/handlers/base.py”   在_get_response中     115. response = self.process_exception_by_middleware(e,request)

     

文件   “ /home/max/Django/area-test/app_usermanager_dev/devenv/lib/python3.6/site-packages/django/core/handlers/base.py”   在_get_response中     113. response = wrapd_callback(request,* callback_args,** callback_kwargs)

     

内部文件“ /usr/lib/python3.6/contextlib.py”     52. return func(* args,** kwds)

     

文件   “ /home/max/Django/area-test/app_usermanager_dev/app_usermanager/usermanager/views.py”   在createUser中     30. profile_form.save()#妥善保存表单

     

文件   “ /home/max/Django/area-test/app_usermanager_dev/devenv/lib/python3.6/site-packages/django/forms/models.py”   在保存     453.如果self.instance._state。添加了其他“ changed”,则“创建”,

     

异常类型:/ create-user /处的ValueError异常值:The   无法更改UserProfile,因为数据未通过验证。

1 个答案:

答案 0 :(得分:2)

在您的UserProfile模型中,有一个字段:

email_address = models.EmailField('Email', unique=True)

添加unique=True时,该字段对于该模型中的所有条目都是唯一的。您可以删除该关键字参数,然后运行makemigrations并进行迁移。或者,您需要为每个UserProfile条目输入唯一的电子邮件地址。


更新:

可能是由于第二次启动时未验证配置文件表单而发生的错误:

profile_form = UserProfileForm(request.POST, instance=user.userprofile) 
profile_form.full_clean()  
# before calling save, you need to call profile_form.is_valid()
profile_form.save()  

TBH,我认为您应该删除帖子保存信号create_user_profilesave_user_profile,因为这会使createUser方法的实现变得过于复杂。另外,如果您没有创建UserProfile的机制,但是有一种形式可以执行此操作,那么后保存信号将非常有用,那么为什么需要一个信号呢?

如果删除这些信号,请尝试以下代码:

def createUser(request):
    if request.method == 'POST':
        user_form = UserCreationForm(request.POST)
        profile_form = UserProfileForm(request.POST or None, request.FILES or None)
        if user_form.is_valid() and profile_form.is_valid():
            user = user_form.save()

            profile_form = profile_form.save(commit=False)
            profile_form.user = user
            profile_form.save()

仅供参考,在按照pep8标准命名方法时,应使用snake_case。因此,最好将createUser命名为create_user