views:

40

answers:

1

I have a FormWizard where I need data from the first form to pass to the constructor of the second form so I can build a dynamic form.

I can get the first form's data via the process_step of the FormWizard.

I create the fields of the second form with a database call of the list of fields.

class ConditionWizardDynamicQuestions(forms.Form):

    def __init__(self, DynamicQuestions=None, *args, **kwargs):
       super(ConditionWizardDynamicQuestions, self).__init__(*args, **kwargs)
       questions = Question.objects.filter(MYDATA = DATA_FROM_1STFORM)
       for q in questions:
            dynField = FieldFactory(q)
            self.fields[q.label] = dynField

How can I pass over the DATA_FROM_1STFORM ?

A: 

I was recently working with django form wizard, and i was solving the similar issue. I don't think you can pass data to init, however, what you can do, is override the init constructor:

next_form = self.form_list[1]

# let's change the __init__
# function which will set the choices :P
def __init__(self, *args, **kw):
    super(next_form, self).__init__(*args, **kw)
    self.fields['availability'].choices = ...
next_form.__init__ = __init__

It's quite annoying that in python you can't declare and assign a function in one go and have to put it in the namespace (unless you use lambdas), but oh well.

cheshire
I'm not sure I follow. Is this code overriding the __init__ of the subclassed FormWizard? How does this allow me to get the data from step1 to step2?
BozoJoe
no, it overrides the __init__ of the next form. Notice how it says self.fields['availability'].choices = ..., now instead of ... you can put any data you want to pass
cheshire
I already have the "next_form" __init__ overrided, so I still don't see how your passing data from the previous form of the wizard.
BozoJoe
Oh, this code has to be inside process_step method of wizard - that way you can get self.form_list[0].cleaned_data, and base the new __init__ on that
cheshire
ok understood, let me give this a try.
BozoJoe