Home > Software engineering >  DJango fill existing formset from View
DJango fill existing formset from View

Time:10-25

I understand that you can fill a form from a view with:

initial_data = { 'box0': 'X', 'box1': 'O', 'box2': 'X' }
form = myform(initial=initial_data)

But, how do you do it for a formset? The below causes error "TypeError: formset_factory() got an unexpected keyword argument 'initial'"

initial_data = []
for i in range(rowCount):
    xy = getRandomXY(3)
    initial_data.append({
        'box0': xy[0],
        'box1': xy[1],
        'box2': xy[2]
    })
formSet = formset_factory(myform, extra=rowCount, max_num=10, initial=initial_data)

CodePudding user response:

Initial is not a parameter of form_factory. It should go inside Formset. Check out django documentation for detail.

formSet = formset_factory(myform, extra=2, max_num=10)
forms = formSet(initial=data)
  • Related