class UserField(forms.EmailField):
def clean(self, value):
super(UserField, self).clean(value)
try:
User.objects.get(username=value)
raise forms.ValidationError("There is an existing account associated with this email.")
except User.DoesNotExist:
return value
try except方法很简单。但是,我很难搞清楚
super(UserField, self).clean(value)
答案 0 :(得分:2)
super(UserField, self).clean(value)
正在调用祖先方法:forms.EmailField.clean(value)
以检查电子邮件是否格式正确。