在Django中动态填充选择字段 [英] Dynamically populating choicefield in Django

查看:221
本文介绍了在Django中动态填充选择字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法在 views.py 内初始化 Choicefield 表单。我尝试在 __ init __ 函数中传递选项变量,但是我收到一个错误:


从$ form = super(SomeeWizard,self)获取至少2个参数(1个给定).get_form(步骤,数据,文件)

  __ init __ 

forms.py



$ $ $ $ $ $ $ $ $ $ $ $ $ $)))))))))))))))))))))))))))))))))))))))))))))))) .__ 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')

编辑



我试过Hasan解决方案,Django表单向导通过 {'files':无,'前缀':'step2','初始':{},'数据':无} ** kwarg __ init __ 函数 SomeForm



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



{'files':None,'prefix':'step2','initial':{},'data':None}
{'choices' :[<选择:蓝色><选择:红色>]}

解决方案

我没有o初始化具有此属性的数据属性的窗体工作。例如:

  class SomeWizard(SessionWizardView):

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

#确定步骤,如果没有给出
if步骤是无:
step = self.steps.current

如果step ==2:
option = self.get_cleaned_data_for_step(1)['choice']

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


##在启动表单时传递数据,即POST
##数据如果在post
##或self.storage.get_step_data(form_key)中调用了get_form函数,如果窗体向导
##在render_done方法中再次验证此窗体
form = SomeForm(choice = choice,data = data)

返回表单

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

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

否则,当提交表单时,它刚刚返回到第一个表单。有关完整解释,请参阅这里。我正在使用django 1.8。


I can't initialize the Choicefield form inside the views.py. I tried passing the option variable in the __init__ function but I got an error:

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

forms.py

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')

EDIT

I tried Hasan solution and Django Form Wizard is passing {'files': None, 'prefix': 'step2', 'initial': {}, 'data': None} into the **kwarg in the __init__ function of SomeForm.

I printed the content in the **kwarg and I got:

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

解决方案

FYI I had to init the form with the data attribute for this to work. For example:

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')

Otherwise when the form was submitted it just returned my back to this first form. For a full explanation see here. I am using django 1.8 though.

这篇关于在Django中动态填充选择字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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