django-formtools icon indicating copy to clipboard operation
django-formtools copied to clipboard

multiple `get_form_initial` calls when `condition_dict` is used

Open adi- opened this issue 6 years ago • 7 comments

Don't know why, but when I use condition_dict, the get_form_initial def is called multiple times.

The above problem is seen when I use wizard.get_cleaned_data_for_step() in condition def.

Any clues?

adi- avatar Jan 08 '19 21:01 adi-

@adi- Can you please tell about the file and line number where you find this error

bvishal8510 avatar Jan 09 '19 17:01 bvishal8510

I am implementing as exampled here: https://django-formtools.readthedocs.io/en/latest/wizard.html#formtools.wizard.views.WizardView.condition_dict

When wizard.get_cleaned_data_for_step('0') is present (not #) in views.py, get_form_initial is initiated multiple times. When you rem this line, everything works as expected.

adi- avatar Jan 09 '19 17:01 adi-

I have also added hooks (simple print statement) for get_form_kwargs, get_form_initial and condition function. I don't see any reason, why those functions are fired multiple times... But only when wizard.get_cleaned_data_for_step is present in condition function.

adi- avatar Jan 09 '19 17:01 adi-

I have the same problem..any solution?

GiuseppePegasus avatar Sep 30 '21 16:09 GiuseppePegasus

Any patch with tests will be welcome!

claudep avatar Sep 30 '21 16:09 claudep

@GiuseppePegasus : as a workaround I use wizard.storage.data['step_data'] to avoid infinite recursion or to fire wizard's methods too early (get_form(), get_form_initial()).

Starou avatar May 11 '22 07:05 Starou

I think the problem is: https://github.com/jazzband/django-formtools/blob/master/formtools/wizard/views.py#L47

This all property is used a lot and recalculates the form_list using the conditions each time it is invoked. I actually had this problem too locally and found out that Django's debug toolbars way of displaying info nicely caused an excessive amount of all invocations. Removing the debug toolbar already significantly improved the behaviour.

It might be nice to generate the form_list once per use of the wizard.

shanx avatar May 09 '23 10:05 shanx