[Django]-How to get data of a previous step in django FormWizard

5๐Ÿ‘

I got it! In order to recieve data from previous step, one must

A) Implement get_form_initial(self, step) for view class W1_Estim

def get_form_initial(self, step):
   if step == '1': 
      # on SECOND step get data of first step
      step0data = self.storage.get_step_data('0')
      if step0data:
         cities = step0data.get('cities', '')
         return self.initial_dict.get(step, {'cities': cities})
   return self.initial_dict.get(step, {})

B) Implement constructor for form class W1_SelectForm

def __init__(self, *args, **kwargs):
   c = kwargs['initial']['cities']
   self.oEstates = ModelMultipleChoiceField( 
      queryset = RealEstate.objects.
      filter(city_id = c).
      order_by('priceM'),
      widget = forms.CheckboxSelectMultiple,
      required = False)
   # NEED TO INCLUDE field oEstates into "declared_fields"!!!
   self.declared_fields['oEstates']=self.oEstates
   # superclass constructor ought to be called only AFTER 
   # including new field of class into "declared_fields" !!!
   super(W1_SelectForm, self).__init__(*args, **kwargs)

Thats all!

๐Ÿ‘คVladimir

2๐Ÿ‘

You can use get_cleaned_data_for_step() method of the wizard view.

class W1_Estim(SessionWizardView):
    template_name = "w1_estim.html"

    def done(self, form_list, **kwargs):
        ...
        data_for_step1 = self.get_cleaned_data_for_step('1')
        #do something with data_for_step1
        ...
๐Ÿ‘คRohan

Leave a comment