【发布时间】:2017-11-02 14:42:36
【问题描述】:
下面是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 个字段,“用户”、“用户名”、“密码”、“电子邮件”。在我的第一个字段“用户”中,我想,我不应该使用“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)
【问题讨论】:
-
不,你说不通。为什么你有那个注册模型?所有这些字段都已经在用户上。拥有单独模型的唯一原因是如果您想存储尚未在 User 模型中的额外信息。除此之外,您不得以纯文本形式存储密码;这就是为什么 User 模型有设置密码的方法。只需完全删除注册。
-
当我删除注册模型并尝试创建超级用户时,我得到“django.db.utils.IntegrityError: UNIQUE constraint failed: auth_user.username”错误
-
SignupForm的定义在哪里?
-
添加到我的原始查询中
-
其实我只是注意到你的问题是关于createsuperuser。所以表单和视图与它没有任何关系;我真的不明白为什么你的标题谈到观点冲突。无论如何,请发布您现在使用的代码和完整的回溯。