我想在formSet中创建第一个表单。我试图在formset类的__init__
方法中实现它,但它不起作用。我收到了这个错误:
__init__() got an unexpected keyword argument 'queryset'
当我使用formset_factory而不是modelformset_factory时,一切都很好。 视图:
form_class = modelformset_factory(EventTime, form = EventTimeForm, extra=3, formset=EventTimeFormset)
formset = form_class(self.request.POST or None, prefix="form", instance = self.object)
形式:
class EventTimeFormset(BaseModelFormSet):
def clean(self):
if any(self.errors):
return
if not self.forms[0].cleaned_data['start_time']:
raise forms.ValidationError("At least 1 time is required for event")
code = "missing_time"
答案 0 :(得分:2)
尝试这样的min_num
和validate_min
-
form_class = modelformset_factory(EventTime, form = EventTimeForm, extra=3, formset=EventTimeFormset, min_num=1, validate_min=True)
这将自动验证至少一个formset工厂项目。
此处记录 -
https://docs.djangoproject.com/en/1.9/topics/forms/formsets/#validate-min