#16446 closed Bug (duplicate)
A WizardView with a step containing a formset generated by inlineformset_factory will always fail
Reported by: | Owned by: | nobody | |
---|---|---|---|
Component: | Forms | Version: | dev |
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 |
Description
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: try: kwargs['initial'] = self.__initial[i] except IndexError: pass return super(BaseStockFormSet, self)._construct_form(i, **kwargs)
And the use inlineformset_factory with formset=BaseInitialFormSet parameter.
Note:
See TracTickets
for help on using tickets.
This is caused by #14574. Closing ad duplicate.