Code

Opened 3 years ago

Closed 3 years ago

#16236 closed New feature (fixed)

Formwizard: computing form initialization keyword arguments

Reported by: zuko Owned by: nobody
Component: contrib.formtools Version: 1.3
Severity: Normal Keywords: wizard
Cc: zuko Triage Stage: Ready for checkin
Has patch: yes Needs documentation: yes
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description

Currently, it isn't possible to provide custom form initialization arguments without overriding the entire get_form() method of wizard view, which is some unnecessary code duplication. So it may be better for WizardView to compute form's kwargs in a separate method, like generic FormView does.

For example, with the patch applied, to initialize form at a step called 'delivery-method' with a city that you have since the 'start' step, you would add these lines to your wizard class definition:

def get_form_kwargs(self, step, data=None, files=None):
    kwargs = super(CheckoutWizard, self)\
        .get_form_kwargs(step, data, files)
    if step == 'delivery-method':
        kwargs['city'] = self.get_cleaned_data_for_step('start')['city']
    return kwargs

I'm unsure whether all these arguments (step, data, files) are necessary for the new method, though. I'm also unsure if use case is common enough, and whether other ways to solve it without lots of code duplication exist. Although it still would be better if WizardView followed FormView's behavior, I guess.

Patch (my first) is pretty small, 5 lines changed (not counting tests and a docstring).

Attachments (1)

formwizard_form_initkwargs.diff (2.8 KB) - added by zuko 3 years ago.

Download all attachments as: .zip

Change History (5)

Changed 3 years ago by zuko

comment:1 Changed 3 years ago by zuko

  • Cc zuko added
  • Easy pickings set
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

comment:2 Changed 3 years ago by aaugustin

  • Needs documentation set
  • Triage Stage changed from Unreviewed to Accepted

comment:3 Changed 3 years ago by jezdez

  • Triage Stage changed from Accepted to Ready for checkin

comment:4 Changed 3 years ago by jezdez

  • Resolution set to fixed
  • Status changed from new to closed

In [16398]:

Fixed #16236 -- Added get_form_kwargs method to WizardView as a way to easily provide kwargs to each step form.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.