我扩展了抽象用户类,但我添加的字段是电话号码,但该字段没有出现在用户更改表单中我会将此字段添加到用户更改表单吗?
答案 0 :(得分:0)
将电话号码字段添加到您的模型和注册表单后,您可以将其添加到 UserUpdateForm 中,如下所示。
class UserUpdateForm(forms.ModelForm):
class Meta:
model = User
fields = ['username', 'email', 'password', 'confirm', 'phone']
username = forms.CharField(label = 'Username')
email = forms.EmailField()
password = forms.CharField(widget = forms.PasswordInput)
confirm = forms.CharField(label = 'Confirm Password', widget = forms.PasswordInput)
phone = forms.CharField() #or if django have a phone field set phonefield
def clean(self):
username = self.cleaned_data.get('username')
password = self.cleaned_data.get('password')
confirm = self.cleaned_data.get('confirm')
email = self.cleaned_data.get('email')
phone = self.cleaned_data.get('phone')
if password and confirm and password != confirm:
raise forms.ValidationError("Passwords doesn't match")
if email == False:
raise forms.ValidationError("A valid Email Address")
values = {
'username' : username,
'password' : password,
'email' : email,
'phone' : phone,
}
return values