Search code examples
djangoformset

how to get initial data on the extra forms


i've got multiple forms, which i want to pull the same initial data into specific fields but only the first formset is doing the job but the rest come blank also note that CustomerForm is a modelform

CustomerFormset = formset_factory(CustomerForm, extra=2)
form = CustomerFormset(initial=[{'Location':'USA', 'age': 10} ])

    context = {  'form':form }
    return render(request,"Customermanager/SubProcess.html",context)

so when the form loads... all the rest of the extra COME BLANK!

i want both my intial plus extra to have the same data if i can get some code how to do it. ill beable to fill in the rest


Solution

  • In order to set different initial values for each form in a formset including the extra forms, you have to pass a list of dictionaries containing the initial data for each form to be rendered.

    Something like the following would do :

    Formset = formset_factory(CustomerForm, extra=2)
    formset = FormSet(initial=[{'Location':'USA', 'age': 10} for x in range(3)])
    

    You might also be interested in This.