Django FormWizard how to change dynamic format

I can dynamically invoke a single form associated with the data that I selected using the ealier step .

But when I am in the done method, I see that my form_list remains unchanged.

here is what i did:

 def get_form_list(request, form_list=None): if form_list is None: form_list = [ProviderForm, DummyForm, ConsummerForm, DummyForm, \ ServicesDescriptionForm] return UserServiceWizard.as_view(form_list=form_list)(request) class UserServiceWizard(SessionWizardView): instance = None def __init__(self, **kwargs): self.form_list = kwargs.pop('form_list') return super(UserServiceWizard, self).__init__(**kwargs) def get_form_instance(self, step): if self.instance is None: self.instance = UserService() return self.instance def get_context_data(self, form, **kwargs): data = self.get_cleaned_data_for_step(self.get_prev_step( self.steps.current)) if self.steps.current == '1': service_name = str(data['provider']).split('Service')[1] form = class_for_name('th_' + service_name.lower() + '.forms', service_name + 'ProviderForm') self.form_list['1'] = form #here my form is correctly change I can see elif self.steps.current == '3': service_name = str(data['consummer']).split('Service')[1] form = class_for_name('th_' + service_name.lower() + '.forms', service_name + 'ConsummerForm') self.form_list['3'] = form context = super(UserServiceWizard, self).get_context_data(form=form, **kwargs) return context def done(self, form_list, **kwargs): print self.form_list #here form_list contains ProviderForm, DummyForm, ConsummerForm, DummyForm, ServicesDescriptionForm 

in step 0, my form_list is fine:

 {u'0': <class 'django_th.forms.wizard.ProviderForm'>, u'1': <class 'django_th.forms.wizard.DummyForm'>, u'2': <class 'django_th.forms.wizard.ConsummerForm'>, u'3': <class 'django_th.forms.wizard.DummyForm'>, u'4': <class 'django_th.forms.base.ServicesDescriptionForm'>} 

in step 1, my form_list is fine: we can see that the second form is my expected

 {u'0': <class 'django_th.forms.wizard.ProviderForm'>, u'1': <class 'th_rss.forms.RssProviderForm'>, u'2': <class 'django_th.forms.wizard.ConsummerForm'>, u'3': <class 'django_th.forms.wizard.DummyForm'>, u'4': <class 'django_th.forms.base.ServicesDescriptionForm'>} 

in step 2, my form_list is ko; same as step 0: my second form returns a DummyForm

 {u'0': <class 'django_th.forms.wizard.ProviderForm'>, u'1': <class 'django_th.forms.wizard.DummyForm'>, u'2': <class 'django_th.forms.wizard.ConsummerForm'>, u'3': <class 'django_th.forms.wizard.DummyForm'>, u'4': <class 'django_th.forms.base.ServicesDescriptionForm'>} 

How can I do to change self.form_list and save the changes I made in get_context_data to the end of the wizard, and not at every step?

EDIT here is the complete code that works great with Rohan's suggestion:

 def get_form(self, step=None, data=None, files=None): """ change the form instance dynamically from the data we entered at the previous step """ if step is None: step = self.steps.current if step == '1': # change the form prev_data = self.get_cleaned_data_for_step('0') service_name = str(prev_data['provider']).split('Service')[1] class_name = 'th_' + service_name.lower() + '.forms' form_name = service_name + 'ProviderForm' form_class = class_for_name(class_name, form_name) form = form_class(data) elif step == '3': # change the form prev_data = self.get_cleaned_data_for_step('2') service_name = str(prev_data['consummer']).split('Service')[1] class_name = 'th_' + service_name.lower() + '.forms' form_name = service_name + 'ConsummerForm' form_class = class_for_name(class_name, form_name) form = form_class(data) else: # get the default form form = super(UserServiceWizard, self).get_form(step, data, files) return form def done(self, form_list, **kwargs): """ Save info to the DB The process is : 1) get the infos for the Trigger from step 0, 2, 4 2) save it to TriggerService 3) get the infos from the "Provider" and "Consummer" services at step 1 and 3 4) save all of them """ # get the datas from the form for TriggerService i = 0 for form in form_list: # cleaning data = form.cleaned_data # get the service we selected at step 0 : provider if i == 0: trigger_provider = UserService.objects.get( name=data['provider'], user=self.request.user) model_provider = get_service_model('provider', data) # get the service we selected at step 2 : consummer elif i == 2: trigger_consummer = UserService.objects.get( name=data['consummer'], user=self.request.user) model_consummer = get_service_model('consummer', data) # get the description we gave for the trigger elif i == 4: trigger_description = data['description'] i += 1 # save the trigger trigger = TriggerService( provider=trigger_provider, consummer=trigger_consummer, user=self.request.user, status=True, description=trigger_description) trigger.save() model_fields = {} # get the datas from the form for Service related # save the related models to provider and consummer i = 0 for form in form_list: model_fields = {} data = form.cleaned_data # get the data for the provider service if i == 1: for field in data: model_fields.update({field: data[field]}) model_fields.update({'trigger_id': trigger.id, 'status': True}) model_provider.objects.create(**model_fields) # get the data for the consummer service elif i == 3: for field in data: model_fields.update({field: data[field]}) model_fields.update({'trigger_id': trigger.id, 'status': True}) model_consummer.objects.create(**model_fields) i += 1 return HttpResponseRedirect('/') 
+6
source share
2 answers

Instead of changing the list of forms, etc. in get_context_data() , I think it would be more appropriate to implement the get_form() method in your wizard view and return an instance of another form depending on the step and previous data.

Something like that:

 class UserServiceWizard(SessionWizardView): instance = None def get_form(self, step=None, data=None, files=None): if step is None: step = self.steps.current prev_data = self.get_cleaned_data_for_step(self.get_prev_step( self.steps.current)) if step == '1': service_name = str(prev_data['provider']).split('Service')[1] form_class = class_for_name('th_' + service_name.lower() + '.forms', service_name + 'ProviderForm') form = form_class(data) elif step == '3': service_name = str(prev_data['consummer']).split('Service')[1] form_class = class_for_name('th_' + service_name.lower() + '.forms', service_name + 'ConsummerForm') form = form_class(data) else: form = super(UserServiceWizard, self).get_form(step, data, files) return form 

The trick here does not change the length of the list of forms (which you did correctly), but just change the instance of the form. For this purpose, Django provided the ability to override the get_form() method. Django will abide by this method and always use it to get an instance of the form for a method.

+5
source

I'm not sure if this is the solution you are looking for, but if you change form_list in process_step and not in get_context_data, it should work. You will have to change your code since process_step is executed after the form is submitted.

According to the Django doc, https://docs.djangoproject.com/en/1.5/ref/contrib/formtools/form-wizard/ process_step is a β€œHook for changing the internal state of wizards”, at least for self.kwargs vars (actually In fact, your form_list is in self.kwargs ["form_list"]) I tested that all changes to get_context_data are ignored, so I believe that self.form_list should behave the same.

0
source

Source: https://habr.com/ru/post/954842/


All Articles