Login

Mutiple forms on Form wizard

Author:
LaraChicharo
Posted:
September 11, 2015
Language:
Python
Version:
1.7
Score:
0 (after 0 ratings)

Wraps many Form subclases in order to get a form wizard to treat them as one.

Many Forms will use one step.

Example:

class LanguageVerifiedHumanForm(MultipleForms): base_forms = [ ('language_form', LanguageForm), ('verified_human_form', VerifiedHumanForm) ]

class NewGuideWizard(SessionWizardView): form_list = [ ('guide_form', GuideForm), ('multi_form', LanguageVerifiedHumanForm), ]

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
from collections import OrderedDict


class MultipleForms(forms.Form):
    """
    Wraps many Form subclases in order to get a form wizard to treat them as one.

    Args:
        base_forms(list): Contains tuples with two elements each: name
        and class of the form we want to add.
    """

    def set_instances(self):
        for name, form in self.base_forms:
            form_instance = form(self.data, self.files, prefix=self.prefix)
            self.instances_list.append(form_instance)
            self.instances_dict.update({name: form_instance})

    def __init__(self, *args, **kwargs):
        for name, form in self.base_forms:
            self.base_fields.update(form.base_fields)
        super(MultipleForms, self).__init__(*args, **kwargs)

        self.instances_dict = OrderedDict()
        self.instances_list = list()
        if self.is_bound:
            self.set_instances()

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 10 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
  5. Help text hyperlinks by sa2812 1 year, 6 months ago

Comments

LaraChicharo (on September 11, 2015):

Not really well tested btw. Dont use save method on this.

#

Please login first before commenting.