我正在尝试通过AbstractUser创建自定义注册表单,但是由于某种原因它没有在数据库中创建条目(基本上注册了新用户)
我的models.py
class UserProfile(AbstractBaseUser):
user = models.OneToOneField(User)
username = models.CharField(max_length=32, blank=False)
name = models.CharField(max_length=128, blank=False)
second_name = models.CharField(max_length=128, blank=False)
middle_name = models.CharField(max_length=128, blank=True)
birthday = models.DateField(null=False)
country = models.CharField(max_length=128)
city = models.CharField(max_length=128)
created = models.DateTimeField(auto_now_add=True)
email = models.EmailField(blank=False, default='youremail@email.com', null=False)
USERNAME_FIELD = 'username'
我的表格.py
from index.models import UserProfile, User
from django import forms
from datetime import datetime
from django.contrib.auth.forms import UserCreationForm
class UserForm(UserCreationForm):
middle_name = forms.CharField(required=False)
class Meta:
model = UserProfile
fields = ['email', 'name', 'second_name', 'password1', 'password2', 'country', 'city', 'birthday']
widgets = {
'birthday': forms.SelectDateWidget(years=range(1900, datetime.now().year + 1))
}
当我尝试测试并注册用户时,出现此错误
null value in column "user_id" violates not-null constraint
DETAIL: Failing row contains (13, 1900-01-01, Switzerland, Montreux, 2018-08-23 12:07:07.367219+00, null, email@email.ch, , Alex, test, None, null, somepasswordhash...).
答案 0 :(得分:0)
您的列user_id
最终为空,我在您的UserProfile
类中看不到它,并且您的表不允许它为空。
您要么必须设置其值,要么使其在表中自动递增(这似乎是您要查找的内容)
希望有帮助~~