[Answer]-How don't call clean_<fieldname> method in 'required=False' field?

1👍

If you have a clean_field, it will be called irrespective of whether you fill it or not because this is internally called by django’s full_clean.

Seems you only want to do your custom validation if some email was provided. So, you can do:

def clean_email(self):
    email = self.cleaned_data['email']
    #If email was provided then only do validation
    if email:
        try:
            User.objects.get(email=self.cleaned_data['email'])
        except ObjectDoesNotExist:
            return email
        else:
            raise forms.ValidationError('Wrong Email!')
    return email

Leave a comment