我正在尝试使用http://yergler.net/blog/2009/09/27/nested-formsets-with-django/添加嵌套内联
为 Django 1.4 使用这些自定义:
django 内联表单集,具有嵌套表单的复杂模型和https://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)
模型.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 %}