Opened 4 years ago

Last modified 19 months ago

#18830 assigned New feature

FormWizard with Formset and Form Fields mixed on same page

Reported by: jeff@… Owned by: Peter Inglesby
Component: Forms Version: 1.4
Severity: Normal Keywords:
Cc: hv@…, thepapermen, Bouke Haarsma, Mathijs de Bruin Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I would like to request something like the following:
On a given page in a FormWizard, support the ability to mix a formset along with other form fields.

A use-case would be a page in a wizard that lets you add N users to email, and a textarea to customize this email.

The code for such a thing would look like

forms.py

# Step 1 in wizard
class Step1Form(forms.Form):
    # interesting form fields...

# Form for the email addresses used in Step 2 Formset
class EmailAddressForm(forms.Form):
    email = forms.EmailField()

# Step 2 in wizard
class EmailAddressesAndText(forms.Form):
    emailText = forms.Textarea()
    emailAddressFormSet = formset_factory(EmailAddressForm, can_delete=True, ...)

in urls.py

## 
named_formwizard_forms = (
    ('Step1', Step1Form),
    ('Step2', EmailAddressesAndText),
)

This would be a very clean way to mix form fields and an embedded formset in a way that can be leveraged as part of the FormWizard found in 1.4

Thanks!

Attachments (1)

form_container.py (4.8 KB) - added by Russell Keith-Magee 4 years ago.
Example of a FormContainer that works with FormWizard

Download all attachments as: .zip

Change History (12)

comment:1 Changed 4 years ago by Russell Keith-Magee

Needs documentation: unset
Needs tests: unset
Patch needs improvement: unset
Resolution: fixed
Status: newclosed
Triage Stage: UnreviewedAccepted

I'm going to mark this as accepted, but with a slightly broader scope than you've given the problem.

This problem exists for form wizards, but it also exists for generic views, too. The root of the problem is that you need to be able to combine forms and formsets (or multiple forms and formsets) on a single page. This is true of a page of a wizard, but it's also true of a "FormView" as well.

Now - the nice thing is that Forms and Formsets share a common interface -- that's why you're able to use a formset as a page in a wizard. To my mind, what we need to do here is write a container object that can wrap multiple forms and formsets,

I've done this on some of my own projects, and the approach works fine; I've attached an example of the code you can use to do it. This needs a lot of work to turn it into a feature, but I think it's a feature worth adding.

Changed 4 years ago by Russell Keith-Magee

Attachment: form_container.py added

Example of a FormContainer that works with FormWizard

comment:2 Changed 4 years ago by Russell Keith-Magee

Resolution: fixed
Status: closedreopened

comment:3 Changed 4 years ago by Thomas Güttler

Cc: hv@… added

I like the idea of a FormContainer very much.

A FormContainer should be able to contain: Forms, FormFields and FormContainers.

comment:4 Changed 4 years ago by Aymeric Augustin

Status: reopenednew

comment:5 Changed 4 years ago by anonymous

Is this now a upcoming feature?

comment:6 Changed 3 years ago by thepapermen

Cc: thepapermen added

comment:7 Changed 3 years ago by Bouke Haarsma

Cc: Bouke Haarsma added

comment:8 Changed 3 years ago by Mathijs de Bruin

Cc: Mathijs de Bruin added

comment:9 Changed 3 years ago by Benoît Bryon

Could Form, FormSet and FormWizard inherit from some base class (Form or BaseForm)?
If they all inherit from a base class, FormView could support them all!

Note: I don't know if is covered by another ticket.
Also, there is a project that aims to make one form out of several: https://pypi.python.org/pypi/django-composite-form/

How do views handle forms?

  • they use the init(), is_valid() and save() methods
  • templates iterate over sets of fields, iterate over fields, or call unicode()
  • isn't it enough?

I mean, from views "point of view", forms, formsets and "composite forms" are really similar.
Wizards may be a bit different.

comment:10 Changed 2 years ago by anonymous

why not allow forms to contain other forms out of the box, like in WTForms?

comment:11 Changed 19 months ago by Peter Inglesby

Owner: changed from nobody to Peter Inglesby
Status: newassigned
Note: See TracTickets for help on using tickets.
Back to Top