谢谢,我正在学习Django,无法弄清楚如何覆盖所有身份验证表单。首先简单说明一下,我有一个自定义用户模型
class PersoUser(AbstractBaseUser):
email = models.EmailField(
verbose_name="Email Adress", max_length=200, unique=True)
username = models.CharField(
verbose_name="username", max_length=200, unique=True)
first_name = models.CharField(verbose_name="firstname", max_length=200)
last_name = models.CharField(verbose_name="lastname", max_length=200)
date_of_birth = models.DateField(verbose_name="birthday")
is_admin = models.BooleanField(default=False)
is_active = models.BooleanField(default=True)
objects = PersoUserManager()
USERNAME_FIELD = "email"
REQUIRED_FIELDS = ["date_of_birth", "username"]
....
,我想将date_of_birth字段添加到我的注册页面,因此我遵循了官方文档,以覆盖所有auth SignupView https://django-allauth.readthedocs.io/en/latest/forms.html#signup-allauth-account-forms-signupform
所使用的specif表单。导致(在Book_store / forms.py中)
from all auth.account.forms import SignupForm from users. models import PersoUser
class PersoUserRegisterForm(SignupForm):
class Meta:
model = PersoUser
fields = ["username", "email", "first_name",
"last_name", "date_of_birth", "password1", "password2"]
def save(self, request):
# Ensure you call the parent class's save.
# .save() returns a User object.
user = super(PersoUserRegisterForm, self).save(request)
# Add your processing here.
# You must return the original result.
return user
在我的settings / base.py
中ACCOUNT_FORMS = {'signup':'Book_store.forms.PersoUserRegisterForm'}
我的account / signup.html模板仅引用{{form.as_p}},并且仅显示默认字段而不显示PersouserRegisterForm中指定的其他字段
我看不到我想念的东西,感谢您阅读
编辑:注册失败,因为它违反了date_of_birth的非空约束
答案 0 :(得分:0)
您要覆盖save方法,但不能使用date_of_birth字段保存用户。
def save(self, request):
user = super(PersoUserRegisterForm, self).save(request)
user.date_of_birth = self.cleaned_data['date_of_birth']
user.save()
return user