【发布时间】:2014-10-13 18:20:26
【问题描述】:
我无法在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)
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')
编辑
我尝试了 Hasan 解决方案,Django 表单向导将 {'files': None, 'prefix': 'step2', 'initial': {}, 'data': None} 传递到 __init__ 的 __init__ 函数中的 **kwargSomeForm。
我打印了**kwarg中的内容,我得到了:
{'files': None, 'prefix': 'step2', 'initial': {}, 'data': None}
{'choices': [<Choice: Blue>, <Choice: Red>]}
【问题讨论】:
-
请发布完整的回溯。
标签: python django django-forms choicefield