Django formsets: make first required?

前端 未结 4 482
独厮守ぢ
独厮守ぢ 2020-11-28 04:05

These formsets are exhibiting exactly the opposite behavior that I want.

My view is set up like this:

def post(request): # TODO: handle vehi         


        
4条回答
  •  伪装坚强ぢ
    2020-11-28 04:41

    Found a better solution:

    class RequiredFormSet(BaseFormSet):
        def __init__(self, *args, **kwargs):
            super(RequiredFormSet, self).__init__(*args, **kwargs)
            for form in self.forms:
                form.empty_permitted = False
    

    Then create your formset like this:

    MyFormSet = formset_factory(MyForm, formset=RequiredFormSet)
    

    I really don't know why this wasn't an option to begin with... but, whatever. It only took a few hours of my life to figure out.

    This will make all the forms required. You could make just the first one required by setting self.forms[0].empty_permitted to False.

提交回复
热议问题