models.py和views.py可能存在冲突

时间:2017-11-02 14:42:37

标签: python django

以下是models.py

的输出
from django.db import models
from django.core.urlresolvers import reverse
#from django.core.urlresolvers import reverse
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.dispatch import receiver

class Registration(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    username = models.CharField(max_length = 250)
    password = models.CharField(max_length = 250)
    email = models.CharField(max_length = 250)

@receiver(post_save, sender=User)
def update_user_profile(sender, instance, created, **kwargs):
    if created:
        Registration.objects.create(username=instance)
    instance.registration.save()

以下是views.py

的输出
from django.shortcuts import render, redirect
from django.contrib.auth import authenticate, login
from django.contrib.auth.forms import UserCreationForm
from .forms import SignUpForm
from django.views import generic

class IndexView(generic.View):
    templet_name = 'user_info/index.html'


def signupview(request):
    if request.method == 'POST':
        form = SignUpForm(request.POST)
        if form.is_valid():
            form.save()
            username = form.cleaned_data.get('username')
            raw_password = form.cleaned_data.get('password')
            user = authenticate(username=username, password=raw_password)
            login(request, user)
            return redirect('registration_form.html')

    else:
        form = SignUpForm()
    return render(request,'user_info/registration_form.html',  {'form': form})

我有两个问题:

1)在我的models.py中,我有4个字段,'用户','用户名','密码''电子邮件' 。在我的第一个字段" user",我想,我不应该使用" models.OneToOneField(User,on_delete = models.CASCADE)",因为,根据我的理解,当我们在工作中使用主键和外键时使用它。如果我错了,请纠正我。

2)在我的views.py,函数" signupview"中,我通过form.save()将表单保存在数据库中,然后清理数据。我做了正确的事情,因为我的models.py有4个字段,但是在view.py中,我只提供了两个字段

username = form.cleaned_data.get('username')
raw_password = form.cleaned_data.get('password')

我希望我在这里有意义。我在通过

创建超级用户时遇到错误
python manage.py createsupseruser

以下是错误

django.db.utils.IntegrityError: NOT NULL constraint failed: user_info_registration.user_id

这就是我提出这些问题的原因。

forms.py

from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm
from django import forms
#from django.forms import ModelForm

class SignUpForm(UserCreationForm):
    #first_name = forms.CharField(max_length=30, required=False, help_text='Optional.')
    #last_name = forms.CharField(max_length=30, required=False, help_text='Optional.')
    email = forms.EmailField(max_length=254, help_text='Required. Inform a valid email address.')

    class Meta:
        model = User
        fields = ('username','password1','email')

models.py的最新外观:

from django.db import models
from django.contrib.auth.models import User

class Registration(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)

1 个答案:

答案 0 :(得分:1)

您收到错误的原因是User发布后保存信号尝试创建Registration实例,但您没有为所有必填字段提供值。

@receiver(post_save, sender=User)
def update_user_profile(sender, instance, created, **kwargs):
    if created:
        Registration.objects.create(username=instance) 
    instance.registration.save()

您必须删除此信号处理程序,或者必须向Registration.objects.create()提供有效参数。