我有以下表格:
class GroupForm(forms.ModelForm):
class Meta:
model = Group
def __init__(self, customer):
self.customer = customer
super(GroupForm, self).__init__()
def clean(self):
cleaned_data = super(GroupForm, self).clean()
email = cleaned_data.get('email')
print email
try:
groups = Group.objects.filter(email=email, customer=self.customer)
if groups:
messsge = u"That email already exists"
self._errors['email'] = self.error_class([messsge])
except:
pass
return cleaned_data
我从视图中调用表单,如下所示:
if request.method == "POST":
form = GroupForm(request.POST, customer, instance=group)
if form.is_valid():
form.save()
问题是验证永远不会被触发。此外,电子邮件的打印永远不会被击中,这意味着 clean 功能永远不会被击中。
为什么会出现这种情况?