Opened 5 years ago

Closed 5 years ago

Last modified 5 years ago

#16446 closed Bug (duplicate)

A WizardView with a step containing a formset generated by inlineformset_factory will always fail

Reported by: guillaume@… Owned by: nobody
Component: Forms Version: master
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no


A WizardView with a step containing a formset generated by inlineformset_factory will always fail.

Indeed inline formset use BaseInlineFormSet class and Forwizard in the method get_form call the instance with the parameter "initial", and the BaseInlineFormSet class doesn't take initial as a parameter.

The solution I have is to create a custom class which inherits from BaseInlineFormSet and take initial as parameter:

class BaseInitialFormSet(BaseInlineFormSet):
    def __init__(self, *args, **kwargs):
        Grabs the curried initial values and stores them into a 'private'
        variable. Note: the use of self.__initial is important, using
        self.initial or self._initial will be erased by a parent class
        self.__initial = kwargs.pop('initial', [])
        super(BaseStockFormSet, self).__init__(*args, **kwargs)

#    def total_form_count(self):
#        return len(self.__initial) + self.extra

    def _construct_forms(self):
        return super(BaseStockFormSet, self)._construct_forms()
        #return formsets.BaseFormSet._construct_forms(self)

    def _construct_form(self, i, **kwargs):
        if self.__initial:
                kwargs['initial'] = self.__initial[i]
            except IndexError:
        return super(BaseStockFormSet, self)._construct_form(i, **kwargs)

And the use inlineformset_factory with formset=BaseInitialFormSet parameter.

Change History (2)

comment:1 Changed 5 years ago by Ramiro Morales

Needs documentation: unset
Needs tests: unset
Patch needs improvement: unset
Resolution: duplicate
Status: newclosed

This is caused by #14574. Closing ad duplicate.

comment:2 Changed 5 years ago by Jacob

milestone: 1.4

Milestone 1.4 deleted

Note: See TracTickets for help on using tickets.
Back to Top