Python 如何在django FormWizard中获取上一步的数据
请向我推荐如何获取向导第一步接收的数据,并以第二步的形式使用数据: forms.pyPython 如何在django FormWizard中获取上一步的数据,python,django,formwizard,Python,Django,Formwizard,请向我推荐如何获取向导第一步接收的数据,并以第二步的形式使用数据: forms.py # First step form class W1_ParamForm(forms.Form): cities = forms.ModelChoiceField( label = 'City', queryset=City.objects.all().order_by('name') ) # Second step form class W1_SelectForm(forms.Fo
# First step form
class W1_ParamForm(forms.Form):
cities = forms.ModelChoiceField( label = 'City',
queryset=City.objects.all().order_by('name') )
# Second step form
class W1_SelectForm(forms.Form):
oEstates = MyModelMultipleChoiceField(
queryset = RealEstate.objects.
filter(city_id__exact = ...).
order_by('priceM'),
widget = forms.CheckboxSelectMultiple,
required = False)
在此…-参数,包含城市id,以第一种形式W1_ParamForm选择。问题是——如何以第二种形式获得它
views.py
class W1_Estim(SessionWizardView):
template_name = "w1_estim.html"
def done(self, form_list, **kwargs):
form_data = [form.cleaned_data for form in form_list]
return render_to_response('w1_done.html', {'form_data': form_data})
url.py
urlpatterns = [
...
# Wizard start
url(r'^doestim$', W1_Estim.as_view([W1_ParamForm,W1_SelectForm]), name='doestim'),
...
非常感谢 您可以使用向导视图的方法
class W1_Estim(SessionWizardView):
template_name = "w1_estim.html"
def done(self, form_list, **kwargs):
...
data_for_step1 = self.get_cleaned_data_for_step('1')
#do something with data_for_step1
...
您可以使用向导视图的方法
class W1_Estim(SessionWizardView):
template_name = "w1_estim.html"
def done(self, form_list, **kwargs):
...
data_for_step1 = self.get_cleaned_data_for_step('1')
#do something with data_for_step1
...
我明白了!为了从上一步接收数据,必须 A) 为视图类W1_Estim实现get_form_initial(self,step)
def get_form_initial(self, step):
if step == '1':
# on SECOND step get data of first step
step0data = self.storage.get_step_data('0')
if step0data:
cities = step0data.get('cities', '')
return self.initial_dict.get(step, {'cities': cities})
return self.initial_dict.get(step, {})
B) 为表单类W1_SelectForm实现构造函数
def __init__(self, *args, **kwargs):
c = kwargs['initial']['cities']
self.oEstates = ModelMultipleChoiceField(
queryset = RealEstate.objects.
filter(city_id = c).
order_by('priceM'),
widget = forms.CheckboxSelectMultiple,
required = False)
# NEED TO INCLUDE field oEstates into "declared_fields"!!!
self.declared_fields['oEstates']=self.oEstates
# superclass constructor ought to be called only AFTER
# including new field of class into "declared_fields" !!!
super(W1_SelectForm, self).__init__(*args, **kwargs)
就这些 我明白了!为了从上一步接收数据,必须 A) 为视图类W1_Estim实现get_form_initial(self,step)
def get_form_initial(self, step):
if step == '1':
# on SECOND step get data of first step
step0data = self.storage.get_step_data('0')
if step0data:
cities = step0data.get('cities', '')
return self.initial_dict.get(step, {'cities': cities})
return self.initial_dict.get(step, {})
B) 为表单类W1_SelectForm实现构造函数
def __init__(self, *args, **kwargs):
c = kwargs['initial']['cities']
self.oEstates = ModelMultipleChoiceField(
queryset = RealEstate.objects.
filter(city_id = c).
order_by('priceM'),
widget = forms.CheckboxSelectMultiple,
required = False)
# NEED TO INCLUDE field oEstates into "declared_fields"!!!
self.declared_fields['oEstates']=self.oEstates
# superclass constructor ought to be called only AFTER
# including new field of class into "declared_fields" !!!
super(W1_SelectForm, self).__init__(*args, **kwargs)
就这些 谢谢,但您的建议不适用,因为“完成”太迟了——必须在阶段中获取第一步的数据,此时第二个表单还没有出现——以便根据第一步的数据进行查询设置。@user3611898,您可以在任何地方调用相同的函数,在完成特定步骤后。我理解它,但我如何在W1_SelectForm的构造函数中接收该数据?如果你能给我解释一下,我将不胜感激!我可能是以错误的方式思考,这是另一种变体来做我需要的?例如,使用W1_SelectForm的某些方法将queryset构造和执行移动到另一个位置?Django(以及Python本身)对我来说绝对是全新的…谢谢,Rohan,你启发了我,我根据你的建议找到了解决方案!我不能投票-没有足够的声誉,对不起…谢谢你,但是你的建议不适用,因为“完成”太晚了-必须在第二个表单还没有出现的阶段获取第一步的数据-以便根据第一步的数据进行查询集。@user3611898,在完成特定步骤后,您可以从任何地方调用相同的函数。我理解,但是我如何在W1_SelectForm的构造函数中接收数据?如果你能给我解释一下,我将不胜感激!也许我的想法是错误的,这是做我需要的事情的另一种变体?例如,使用W1_SelectForm的某些方法将queryset构造和执行移动到另一个位置?Django(以及Python本身)对我来说绝对是全新的…谢谢,Rohan,你启发了我,我根据你的建议找到了解决方案!我不能投票-没有足够的声誉,对不起。。。