Search code examples
djangopython-3.xdjango-formtools

How to manually reset the data in a step of a formtool wizard?


I have a wizard with several steps organized as follow:

1 -> 2 -> 3 -> 4 -> 6
          |         ^
          |         |
           --> 5 ---

where step 6 is just a review of the data in previous steps. Steps 4 and 5 are mutually exclusive.

If a user travels the form 1,2,3,4,6 and then decides to use 5 instead of 4 I want to be able to reset the data in step 4. How can I manually reset the data already stored for step 4 (or any step) of the wizard?


Solution

  • This is not really documented in django-formtools, but you'll find that the WizardView has a property self.storage which is an instance of BaseStorage (in 'formtools.wizard.storage.base').

    self.storage.data is a dictionary of all the stored data. It's a bit dangerous to manipulate this dictionary directly, better use the method self.storage.set_step_data(step, data) to change the data for specific step:

    self.storage.set_step_data('4', {})
    

    will empty the data for step '4'.

    Note: If you're also uploading files, you should remove them, which is a bit tricky, because self.storage.set_step_files(step, files) doesn't do anything if files is empty ({}). Look at that method to either override it or see how to remove the files from the data dictionary.