I have a problem with formwizard. In step 3 of the wizard, I use a filtered query, which depends on the choice from steps 1 and 2. My wizard works fine when moving forward, but when the user clicks the previous one or changes the choice in steps 1 and 2, everything goes wrong (validation error or filters will not work).
Here are my forms.py:
class BoekForm3(forms.Form): Activiteit = forms.ModelChoiceField(queryset=Activiteit.objects.all(), empty_label="(Nothing)")
If I use queryset Activiteit.objects.none () in forms.py, the correct fields in step 3 of the wizard are displayed on the right, but step3 always gives a validation error.
If I use queryset Activiteit.object.all () in forms.py, the correct (filtered) fields are displayed in step 3 when going forward, and the form is validated. However, when the user clicks the previous step 4, the filter disappears and all options are available.
Edit2: ok I found out that get_form is read every time (removed my previous edit about it). The problem with my code is that when the user returns from step 4 to step 3, the get_form step is used, this is step4. I tried to change:
"if step == 'step3'"
in:
"if step == 'step3' or step == 'step4':"
in my opinion, but this leads to a key error in Activiteit
I think the fields are updated by going to step 3, but there is no form. I have included my view.py below.
class BoekingWizard(SessionWizardView): def get_template_names(self): return [TEMPLATES[self.steps.current]] def get_context_data(self, form, **kwargs): plaatjes = Activiteitsgroepen.objects.all() context = super(BoekingWizard, self).get_context_data(form=form, **kwargs) if self.steps.current == 'step1': context.update({'plaatjes': plaatjes}) return context def get_form(self, step=None, data=None, files=None): form = super(BoekingWizard, self).get_form(step, data, files) if step == 'step3': x = self.get_cleaned_data_for_step('step1') or {} filter1 = x['Type_activiteit'].Groep y = self.get_cleaned_data_for_step('step2') or {} if y['Aantal_personen'] < 76: filter2 = 'kleinere groepen' elif y['Aantal_personen'] > 76: filter2 = 'grotere groepen' else: filter2 = 'geen selectie' reeks = ['1','2','3'] if y['Dagdeel'] in reeks: filter3 = 'Dagdeel' elif y['Dagdeel'] == '4': filter3 = 'Hele dag' elif y['Dagdeel'] == '5': filter3 = 'Middag + Avond' elif y['Dagdeel'] == '6': filter3 = 'Dag + Avond' form.fields['Activiteit'].queryset = Activiteit.objects.filter(Soort__Groep = filter1).filter(Grootte__Naam = filter2).filter(Dag__Omschrijving = filter3) return form