generic_inlineformset_factory不会将GenericForeignKey显示到模板中

时间:2013-06-28 07:36:26

标签: django django-models django-forms django-templates django-views

在我的Django-1.5.1应用程序中,我有以下情况:

  • models.py

    class Cod(models.Model):
        name = models.SlugField()
        user = models.ForeignKey(User)
    
    class Steps(models.Model):
        number = models.PositiveSmallIntegerField()
        cod = models.ForeignKey(Cod, related_name='cod')
        step_cod = models.ForeignKey(Cod, null=True, related_name='step_cod')
        content_type = models.ForeignKey(ContentType)
        object_id = models.PositiveIntegerField()
        content_object = generic.GenericForeignKey()
    
  • forms.py

    class StepsForm(ModelForm):
        class Meta:
           model = Steps
    
  • views.py

    def getSteps(request):
        cod_id = 1
        steps = Steps.objects.filter(cod=cod_id).order_by('number')
        StepFormset = generic_inlineformset_factory(Steps, form=StepsForm, can_delete=False)
        step_formset = StepFormset(queryset=steps)
        for step in steps:
            print step.content_object
        print "-------------------------"
        for form in step_formset:
            print form.instance
        print "-------------------------"
        for form in step_formset:
            print form.instance.content_object
    
        return render_to_response('tss/base_codForm.html',
                                 {'step_formset': step_formset},
                                  context_instance=RequestContext(request))
    
  • base_codForm.html

    {% for form in step_formset %}
        {{ form }}
    {% endfor %}
    

查看 views.py 中的for循环输出,我得到了正确的相关对象,我的formset中的步骤实例,但缺少表单实例中的content_object:

ObjectAggregate object
ObjectGroup object
ObjectRefine object
ObjectSizeCheck object
-------------------------
Steps object
Steps object
Steps object
-------------------------
None
None
None

确实,我的模板只呈现number,cod和step_cod字段:

<tr>
    <th><label for="id_tss-steps-content_type-object_id-0-number">Number:</label></th>
    <td><input id="id_tss-steps-content_type-object_id-0-number" name="tss-steps-content_type-object_id-0-number" type="text" /></td>
</tr>
<tr>
    <th><label for="id_tss-steps-content_type-object_id-0-cod">Cod:</label></th>
    <td>
        <select id="id_tss-steps-content_type-object_id-0-cod" name="tss-steps-content_type-object_id-0-cod">
            <option value="" selected="selected">---------</option>
            <option value="1">Cod object</option>
        </select>
    </td>
</tr>
<tr>
    <th><label for="id_tss-steps-content_type-object_id-0-step_cod">Step cod:</label></th>
    <td>
        <select id="id_tss-steps-content_type-object_id-0-step_cod" name="tss-steps-content_type-object_id-0-step_cod">
            <option value="" selected="selected">---------</option>
            <option value="1">Cod object</option>
        </select>
        <input id="id_tss-steps-content_type-object_id-0-id" name="tss-steps-content_type-object_id-0-id" type="hidden" />
    </td>
</tr>

<tr>
    <th><label for="id_tss-steps-content_type-object_id-1-number">Number:</label></th>
    <td><input id="id_tss-steps-content_type-object_id-1-number" name="tss-steps-content_type-object_id-1-number" type="text" /></td>
</tr>
<tr>
    <th><label for="id_tss-steps-content_type-object_id-1-cod">Cod:</label></th>
    <td>
        <select id="id_tss-steps-content_type-object_id-1-cod" name="tss-steps-content_type-object_id-1-cod">
            <option value="" selected="selected">---------</option>
            <option value="1">Cod object</option>
        </select>
    </td>
</tr>
<tr>
    <th><label for="id_tss-steps-content_type-object_id-1-step_cod">Step cod:</label></th>
    <td>
        <select id="id_tss-steps-content_type-object_id-1-step_cod" name="tss-steps-content_type-object_id-1-step_cod">
            <option value="" selected="selected">---------</option>
            <option value="1">Cod object</option>
        </select>
        <input id="id_tss-steps-content_type-object_id-1-id" name="tss-steps-content_type-object_id-1-id" type="hidden" />
    </td>
</tr>

<tr>
    <th><label for="id_tss-steps-content_type-object_id-2-number">Number:</label></th>
    <td><input id="id_tss-steps-content_type-object_id-2-number" name="tss-steps-content_type-object_id-2-number" type="text" /></td>
</tr>
<tr>
    <th><label for="id_tss-steps-content_type-object_id-2-cod">Cod:</label></th>
    <td>
        <select id="id_tss-steps-content_type-object_id-2-cod" name="tss-steps-content_type-object_id-2-cod">
            <option value="" selected="selected">---------</option>
            <option value="1">Cod object</option>
        </select>
    </td>
</tr>
<tr>
    <th><label for="id_tss-steps-content_type-object_id-2-step_cod">Step cod:</label></th>
    <td>
        <select id="id_tss-steps-content_type-object_id-2-step_cod" name="tss-steps-content_type-object_id-2-step_cod">
            <option value="" selected="selected">---------</option>
            <option value="1">Cod object</option>
        </select>
        <input id="id_tss-steps-content_type-object_id-2-id" name="tss-steps-content_type-object_id-2-id" type="hidden" />
    </td>
</tr>

我错过了什么?如何在我的formset中获取相关的通用对象? 谢谢。

1 个答案:

答案 0 :(得分:-1)

似乎models.py应该是:

class Steps(models.Model):
    ...
    content_type = models.ForeignKey(ContentType)
    object_id = models.PositiveIntegerField()
    content_object = generic.GenericForeignKey('content_type', 'object_id')

此外,您可以查看this decision