如何在FormWizard中将以前的表单数据传递给DynamicForm的构造函数 [英] How to pass previous form data to the constructor of a DynamicForm in FormWizard

查看:103
本文介绍了如何在FormWizard中将以前的表单数据传递给DynamicForm的构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个FormWizard,我需要将第一种表单中的数据传递给第二种表单的构造函数,以便构建动态表单.

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.

我可以通过FormWizard的process_step获取第一个表单的数据.

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

如何跳过 DATA_FROM_1STFORM ?

我的结果代码: 我放弃了表单的 init ,并将其切换为CreateQuestions定义.然后,使用向导的get_form覆盖在创建后更改表单.

my resultant code: I abandoned the init of the form, and switched it to the CreateQuestions def. Then used the wizard's get_form override to alter the form after creation.

class ConditionWizard(SessionFormWizard):
    def get_form(self, request, storage, step=None, data=None, files=None):
        form = super(ConditionWizard, self).get_form(request, storage, step, data, files)
        stepIndex = self.get_step_index(request, storage, step)
        if stepIndex == 1:
            form.CreateQuestions(request.session["WizardConditionId"])
        if stepIndex == 3:
            form.fields['hiddenConditionId'].initial = request.session["WizardConditionId"]
            form.fields['medicationName'].queryset = Medication.objects.filter(condition = request.session["WizardConditionId"])
        return form

推荐答案

FormWizard已将数据从每个先前的表单传递到下一个表单.如果要获取该数据以便实例化一个类(例如,如果表单具有所需的特殊关键字参数),一种实现方法是通过重写表单向导类中的get_form来获取querydict.例如:

FormWizard already passes the data from each previous form to the next form. If you want to get that data in order to instantiate a class (for example, if a form has special keyword arguments that it requires), one way of doing it is to grab the querydict by overriding get_form in your form wizard class. For example:

class SomeFormWizard(FormWizard):
    def get_form(self, step, data=None):
        if step == 1 and data: # change this to whatever step requires
                               # the extra data
            extra_data = data.get('key_from_querydict')
            if extra_data:
                return self.form_list[step](data,
                                            keyword_argument=extra_data,
                                            prefix=self.prefix_for_step(step),                                                                                                                                            
                                            initial=self.initial.get(step, None))
        # Fallback for the other forms.
        return self.form_list[step](data,
                                    prefix=self.prefix_for_step(step),                                                                                                                                            
                                    initial=self.initial.get(step, None))

请注意,您也可以像在视图中一样覆盖FormWizard中的parse_params(self, request, *args, **kwargs)以访问url/请求数据,因此,如果您有需要的请求数据(例如,request.user)对于所有表单,最好从那里获取数据.

Note that you can also override parse_params(self, request, *args, **kwargs) in FormWizard to access the url/request data, just like you would in a view, so if you have request data (request.user, for instance) that is going to be needed for all of the forms, it might be better to get the data from there.

希望这会有所帮助.

这篇关于如何在FormWizard中将以前的表单数据传递给DynamicForm的构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆