Django: How to add an extra form to a formset after it has been constructed?

后端 未结 4 1173
误落风尘
误落风尘 2021-02-01 21:44

This is roughly what I\'m trying to do:

def post(request):
    VehicleFormSet = formset_factory(StaffVehicleForm)
    if request.method == \'POST\':
        vehi         


        
4条回答
  •  故里飘歌
    2021-02-01 22:19

    I do this using javascript. Since the formset renders three management fields

    
    .
    
    

    you can use javascript to increment the id_TOTAL_FORMS value, and just add in the extra fields. So I'd create my fieldset like this:

    VehicleFormSet = modelformset_factory(StaffVehicleForm, extra = 0, max_num = None)
    

    The tricky thing is to create the extra form fields in javascript. I usually use AJAX to fetch a new row from a custom view.

提交回复
热议问题