我可以添加地址,城市,州,国家/地区,密码和安全问题等字段,以及当前包含用户名,电子邮件,密码1和密码2的UserCreationForm的扩展名。如果是,请说明如何?
forms.py
class UserCreationForm(UserCreationForm):
email = EmailField(label=_("Email address"), required=True,
help_text=_("Required."))
city= forms.CharField(label= _("City"),max_length=20, required=True)
state= forms.CharField(label= _("State"),max_length=20, required=True)
class Meta:
model = User
fields = ("username", "email", "password1", "password2","city","state")
def save(self, commit=True):
user = super(UserCreationForm, self).save(commit=False)
user.email = self.cleaned_data["email"]
user.city = self.cleaned_data["city"]
user.state = self.cleaned_data["state"]
if commit:
user.save()
return user
答案 0 :(得分:2)
是的就像你用电子邮件做的那样:
class UserCreationForm:
a_field = WhateverField(whatever='whatever'...)
class Meta:
model = User
fields = ("username", "email", "password1", "password2")
该字段现已添加到您的表单中。
答案 1 :(得分:1)
目前您正在扩展UserCreationForm类,如果您在用户模型中有其他字段,那么您可以使用forms.ModelForm并只提及其他字段。
例如
class UserForm(forms.ModelForm):
class Meta:
model = User
fields = ['username','name', 'email', 'phone',
'address','city','state', 'first_time']
如果你想使用自定义表格。
class UserForm(forms.Form):
name = forms.CharField(label= _("Name"))
address = forms.CharField(label=_("Address"))
etc.