好的,所以我有 UserUpdateForm 和 RegistrationForm。目前每个都有这个功能:
def clean_email(self):
email = self.cleaned_data.get('email')
if email and User.objects.filter(email=email).exclude(pk=self.instance.id).count():
raise forms.ValidationError('Email already in use.')
return email
我想知道避免这种重复的理想方法是什么。
请指教。
** 更新 **
如果我需要调用父函数但所有一些东西,说我有这个怎么办:
def clean_email(self):
email = self.cleaned_data.get('email')
if email and User.objects.filter(email=email).exclude(pk=self.instance.id).count():
raise forms.ValidationError('Email already in use.')
### THIS BIT IS ONLY NEEDED IN ONE OF THE CHILD FORMS ###
# Check whether the email was change or not
if self.instance.email != email:
# To not change the email in our database until the new one is verified
return self.instance.email
###
return email