0

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

  • 模型.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()
    
  • 表格.py

    class StepsForm(ModelForm):
        class Meta:
           model = Steps
    
  • 视图.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 循环输出,我得到了正确的相关对象,即我的表单集中的步骤实例,但缺少表单实例中的 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>

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

4

1 回答 1

-1

似乎models.py应该是:

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

另外你可以看看这个决定

于 2013-06-28T17:59:32.013 回答