我已经构建了转换表单集,并且我想进行验证,因此在表单集中没有双重项目预设可以使用clean。但是当表单集中存在一个空表单时,它将产生错误。这是modelformset_factory
错误消息:
KeyError at /saleformformset_update/1024'item
我的代码:
class SaleTransictionFormSet(BaseModelFormSet):
def clean(self):
super().clean()
"""Checks that no two articles have the same title."""
if any(self.errors):
# Don't bother validating the formset unless each form is valid on its own
return
titles = []
for form in self.forms :
title = form.cleaned_data['item']
if not title == '':
if title in titles:
raise forms.ValidationError(" %s in a set must have distinct titles." % title)
titles.append(title)
如果它们为空或被标记为删除,我想干净地忽略表单集中的表单。