Django formsets: make first required?

前端 未结 4 479
独厮守ぢ
独厮守ぢ 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:43

    Oh I think I see. Try this:

    from django.forms.formsets import BaseFormSet, formset_factory
    class OneExtraRequiredFormSet(BaseFormSet):
        def initial_form_count(self):
            return max(super(OneExtraRequiredFormSet,self).initial_form_count() - 1,0)
    
    VehicleFormSetFactory = formset_factory(VehicleForm, formset=OneExtraRequiredFormSet, extra=1)
    

    == Original answer below ==

    When you say "at least make one form required", I assume you mean "make only one extra form required, regardless of how many have been added via javascript".

    You will need to have hidden input on your page which contains the number of forms that have been added via javascript, and then use that number, minus 1, as the value to pass in as the extra attribute to your formsets constructor.

提交回复
热议问题