尝试实现嵌套表单集的管理错误和__init__错误

时间:2013-08-06 22:46:06

标签: django

我正在尝试使用http://yergler.net/blog/2009/09/27/nested-formsets-with-django/

添加嵌套的内联

使用Django 1.4的这些自定义:

django inline formsets with a complex model for the nested formhttps://groups.google.com/forum/#!topic/django-users/E1oL0khsUlI

但是,根据form.nested代码,我会得到不同的错误。

要么:

[u'ManagementForm data is missing or has been tampered with']

__init__() got an unexpected keyword argument 'instance'

解释一切的代码如下:

** forms.py ** 产生此错误

__init__() got an unexpected keyword argument 'instance'

form.nested = [
         IndicatorValueFormSet(
              data=self.data or None,
              instance=instance,
              prefix = 'indicator_%s' % pk_value)]

IndicatorValueFormSet = modelformset_factory(IndicatorValue, extra=0)
IndicatorFormSet = inlineformset_factory(Disease, Indicator, formset=BaseIndicatorFormSet, extra=1)
下面的

代码会产生此错误:

[u'ManagementForm data is missing or has been tampered with']

form.nested = [
          IndicatorValueFormSet(
               queryset = IndicatorValue.objects.filter(relevantindicator = pk_value), 
               prefix = 'value_%s' % pk_value)]

IndicatorValueFormSet = modelformset_factory(IndicatorValue, extra=0)
IndicatorFormSet = inlineformset_factory(Disease, Indicator, formset=BaseIndicatorFormSet, extra=1)

models.py

class Disease(models.Model):
        disease = models.CharField(max_length=300, verbose_name="disease")


class Indicator(models.Model):
        relevantdisease = models.ForeignKey(Disease, verbose_name="disease")       
        indicator = models.CharField(max_length=300, verbose_name="indicator")

class IndicatorValue(models.Model):
        relevantindicator = models.ForeignKey(Indicator, verbose_name="indicators")
        indicator_value = models.CharField(max_length=50, verbose_name="indicator Values")

更新

HTML

 {{disease.management_form}}

 {{ indicators.management_form }}

{% for relevantindicator in indicators.forms %}

  {{ relevantindicator }}

  {% if relevantindicator.nested %}
  {% for formset in relevantindicator.nested %}
  {{ formset.as_table }}
  {% endfor %}
  {% endif %}

{%endfor%}

1 个答案:

答案 0 :(得分:0)

这是因为,Django在表单中添加了与管理formset相关的数据。您需要包含它,因为您手动渲染表单。

请参阅:django modelformset_factory - management form data is missing

PS:简单的Google或Stack Overflow搜索就可以得到你的答案。