[Answered ]-Django form wizard validate (clean) form using previous form

2πŸ‘

βœ…

It can be achieved as follows:

  1. Overwrite the get_form_initial(self, step) method of the wizard. Handle the later step as follows:
    1. retrieve the cleaned data of the earlier step;
    2. retrieve the initial data for this step (super call);
    3. merge the two dictionaries and return the result;
  2. In the clean method of the later form access the data of the earlier form via the self.initial field.

Code snippet for the Wizard class:

def get_form_initial(self, step):
    if step == '5':
        step4 = self.get_cleaned_data_for_step('3')
        res = super(DenovoPatternWizard, self).get_form_initial(step)
        res['extendsequencedb'] = {}
        res['extendsequencedb']['include_most_similar_pattern_sequences'] = step4['sequencepatternassignment'][
            'number_sites_per_pattern']
        return res

Code snippet of the later form:

def clean(self):
    cleaned_data = super(InputDenovoPatternPatternForm, self).clean()
    # compatibility of earlier and current step

    if self.initial['timeseries_data'].number_timepoints != cleaned_data['pattern_data'].number_timepoints:
        raise ValidationError("The time-series and pattern input files need to have the same number of timepoints (%d and %d respectively)." %
                              (self.initial['timeseries_data'].number_timepoints, cleaned_data['pattern_data'].number_timepoints))
πŸ‘€derwiwie

Leave a comment