我EmailHistory
使用如下所示的字段之一调用模型:
from_email = models.EmailField(verbose_name="From:")
我创建了一个 ModelForm 并希望验证电子邮件地址,除非它设置为“匿名”。我尝试了以下方法,但无济于事。
class EmailForm(ModelForm):
class Meta:
model = EmailHistory
exclude = ('to_email')
to_emails = forms.CharField()
def clean_from_email(self):
from_email = self.cleaned_data['from_email']
if from_email == "Anonymous":
return from_email
else:
return super(EmailForm, self).clean_from_email();