「required=False」フィールドで clean_ メソッドを呼び出さない方法は?
フォーム.py
from django import forms
class userRegistrationForm(forms.Form):
username = forms.CharField(
max_length=30,
)
password1 = forms.CharField(
widget=forms.PasswordInput(),
)
email = forms.EmailField( # If user fill this, django will call 'clean_email' method.
required=False, # But when user don't fill this, I don't want calling 'clean_email' method.
)
def clean_email(self):
if 'email' in self.cleaned_data:
try:
User.objects.get(email=self.cleaned_data['email'])
except ObjectDoesNotExist:
return self.cleaned_data['email']
else:
raise forms.ValidationError('Wrong Email!')
ユーザーは「email」に入力でき、「email」フィールドに入力する必要はありません。
私は何をすべきか?