繁体   English   中英

在Django中动态填充选择字段

[英]Dynamically populating choicefield in Django

我无法在views.py初始化Choicefield表单。 我尝试在__init__函数中传递option变量,但出现错误:

__init__() takes at least 2 arguments (1 given)` coming from the `form = super(SomeeWizard, self).get_form(step, data, files)

表格

class SomeForm(forms.Form):
        def __init__(self, choice, *args, **kwargs):
            super(SomeForm, self).__init__(*args, **kwargs)
            self.fields['choices'] = forms.ChoiceField(choices=[ (o.id, str(o)) for o in choice])

views.py

class SomeWizard(SessionWizardView):

    def get_form(self, step=None, data=None, files=None):
        form = super(SomeWizard, self).get_form(step, data, files)

        if step == "step2":
            option = self.get_cleaned_data_for_step("step1")['choice']

            choice = Choice.objects.filter(question__text__exact=option)

            form = SomeForm(choice)

        return form

    def get_template_names(self):
        return [TEMPLATES[self.steps.current]]

    def done(self, form_list, **kargs):
        return render_to_response('done.html')

编辑

我试图哈桑溶液和Django表单向导传递{'files': None, 'prefix': 'step2', 'initial': {}, 'data': None}**kwarg__init__的功能SomeForm

我将内容打印在**kwarg ,得到了:

{'files': None, 'prefix': 'step2', 'initial': {}, 'data': None} {'choices': [<Choice: Blue>, <Choice: Red>]}

尝试此更改(我评论更改的行):

forms.py

class SomeForm(forms.Form):
    def __init__(self, *args, **kwargs): #this line changed
        choice = kwargs.pop('choice', None) #this line added
        self.fields['choices'] = forms.ChoiceField(choices=[ (o.id, str(o)) for o in choice])
        super(SomeForm, self).__init__(*args, **kwargs)

views.py

class SomeWizard(SessionWizardView):

    def get_form(self, step=None, data=None, files=None):
        form = super(SomeWizard, self).get_form(step, data, files)

        if step == "step2":
            option = self.get_cleaned_data_for_step("step1")['choice']

            choice = Choice.objects.filter(question__text__exact=option)

            form = SomeForm(choice=choice) #this line changed

        return form

    def get_template_names(self):
        return [TEMPLATES[self.steps.current]]

    def done(self, form_list, **kargs):
        return render_to_response('done.html')

仅供参考,我必须使用data属性初始化表单才能正常工作。 例如:

class SomeWizard(SessionWizardView):

    def get_form(self, step=None, data=None, files=None):
        form = super(SomeWizard, self).get_form(step, data, files)

        # determine the step if not given
        if step is None:
            step = self.steps.current

        if step == "2":
            option = self.get_cleaned_data_for_step("1")['choice']

            choice = Choice.objects.filter(question__text__exact=option)


            ## Pass the data when initing the form, which is the POST
            ## data if the got_form function called during a post
            ## or the self.storage.get_step_data(form_key) if the form wizard
            ## is validating this form again in the render_done methodform 
            form = SomeForm(choice=choice, data=data) 

        return form

def get_template_names(self):
    return [TEMPLATES[self.steps.current]]

def done(self, form_list, **kargs):
    return render_to_response('done.html')

否则,在提交表单后,它将返回我的第一个表单。 有关完整说明,请参见此处 我正在使用django 1.8。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM