【发布时间】:2013-09-17 03:43:24
【问题描述】:
我有以下表格:
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 功能永远不会被点击。
为什么会这样?
【问题讨论】:
标签: django django-forms django-validation