【问题标题】:Django 1.4 formwizard with a formset带有表单集的 Django 1.4 表单向导
【发布时间】:2012-07-13 11:28:31
【问题描述】:

我想在表单向导中使用表单集。

class Model1(models.Model):
    customerid = models.CharField(max_length=20)
    Name = models.CharField(max_length=40)

class Model1Form(ModelForm):
    class Meta:
            model = Model1

class Model2(models.Model):
    product = models.CharField(max_length=100)
    price = models.DecimalField(max_digits=5, decimal_places=2)

class Model2Form(forms.Form):
    product = forms.ModelChoiceField(queryset=Model2.objects.all())
    amount = forms.IntegerField(required=False)

Model2Formset = formsets.formset_factory(Model2Form)

在我的 urls.py 中:

    (r'^testwizard/$', TestWizard.as_view([Model1Form, Model2Formset])),

我使用基本视图查看发布表单的结果:

class TestWizard(SessionWizardView):
def done(self, form_list, **kwargs):
    return render_to_response('template', {
        'form_data': [form.cleaned_data for form in form_list],
    })

当表单集有多个条目时,我只能看到一个条目:

 {'customerid': u'7676', 'Name': u'7', 'klantnummer': u'7'} [{'product': <Model2: Bike>, 'amount': 7}]

我预计:

{'customerid': u'7676', 'Name': u'7', 'klantnummer': u'7'} [{'product': <Model2: Bike>, 'amount': 7},{'product': <Model2: Plane>, 'amount': 5}]

在文档中可以找到:

WizardView 支持 ModelForms 和 ModelFormSets。除了 initial_dict 之外,>as_view() 方法采用 instance_dict 参数,该参数应包含 >ModelForm 和 ModelFormSet 的实例。与initial_dict类似,这些字典键值应该>等于表单列表中的步数。

很遗憾,我不确定这里是什么意思。

【问题讨论】:

  • 我忘了提到我使用jQuery formset 来渲染模板中的额外字段。只有表单集(没有表单向导)一切正常。

标签: django django-formwizard


【解决方案1】:

这不起作用,因为我需要一个内联表单集记录在这里: https://docs.djangoproject.com/en/dev/topics/forms/modelforms/#using-an-inline-formset-in-a-view

【讨论】:

    猜你喜欢
    • 2015-08-19
    • 2012-07-08
    • 2013-07-24
    • 2014-12-27
    • 1970-01-01
    • 2021-03-10
    • 2013-08-12
    • 2011-09-17
    • 1970-01-01
    相关资源
    最近更新 更多