Django formwizard with formsets and forms on same

2019-08-14 02:44发布

I'm working with Django 1.4 FormWizard (specifically, NamedUrlFormWizard)

first, the basics. i have a 3 step form wizard i'm building. The final outcome is along the lines of defining a template, and then choosing some people to use it, and then send them an email.

  • Step 1 - enter in basic template data (name, description, etc)
  • Step 2 - define a list of N fields, each with their own set of attributes but all identical in structure
  • Step 3 - choose one or more users to email, AND customize the contents of this email before saving

so far, in the form wizard:

  • Step 1 is a standard Form class, and works just fine.
  • Step 2 is a standard Formset class, and works just fine.
  • Step 3 is giving me some trouble. It needs to be a formset (list of email addresses), but also an additional form input field with email text. I can't figure out how to have both a formset in addition to a non-repeating form input on the same page inside of a form wizard.

in a perfect world, i could define a Formset as just another form field in a Form definition. ie:

class EmailAddressForm(forms.Form):
    email = forms.EmailField()

class EmailAddressesAndText(forms.Form):
    emailText = forms.Textarea()
    emailAddressFormSet = formset_factory(EmailAddressForm, etc etc)

then point my FormWizard page at 'EmailAddressesAndText' and be done with it. but its not a perfect world. Any ideas on how I can achieve such a thing?

1条回答
我想做一个坏孩纸
2楼-- · 2019-08-14 03:35

You can use get_form() method of WizardView to customize the form for particular step. Refer: WizardView.get_form. This answer can help you to add a field

Another option would be to add the field in formset and through JS disable and hide all instances other than first one.

查看更多
登录 后发表回答