cleaned_data
您应该检查每个条目并确认所有值都是空的,而不是检查的长度(它应该始终包含每个表单字段的一个条目)。
这是一个如何做到这一点的例子。
def clean(self):
cleaned_data = super(SearchForm, self).clean()
form_empty = True
for field_value in cleaned_data.itervalues():
# Check for None or '', so IntegerFields with 0 or similar things don't seem empty.
if field_value is not None and field_value != '':
form_empty = False
break
if form_empty:
raise forms.ValidationError(ugettext_lazy("You must fill at least one field!"))
return cleaned_data # Important that clean should return cleaned_data!