OK, so I have UserUpdateForm and RegistrationForm. Each with this function currently:
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
I was wondering what would be the ideal way to avoid this duplication.
Please advise.
** UPDATE **
What if I needed to call the parent function but all some stuff to it, say I have this:
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