私の 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()
フォーム.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>
私は何が欠けていますか?フォームセット内で関連する汎用オブジェクトを取得する方法は? ありがとうございました。