Django formset访问模板中的初始数据

jvc*_*c26 1 django django-templates django-forms

我有一个Django formset,其中包含一组初始数据,它将一个foreignkey关系对象加载到初始形式:

{{ cellcountformset.management_form }}
{% for form in cellcountformset %}
<div id="">
    {{ form.errors }}
    {{ form.as_p }}
</div>
{% endfor %}
Run Code Online (Sandbox Code Playgroud)

相关模型如下所示:

class CellType(models.Model):
    readable_name = models.CharField(max_length=50)
    machine_name = models.CharField(max_length=50)
    comment = models.TextField(blank=True)

class CellCount(models.Model):
    cell_count_instance = models.ForeignKey(CellCountInstance)
    cell = models.ForeignKey(CellType)
    normal_count = models.IntegerField()
    abnormal_count = models.IntegerField()
    comment = models.TextField(blank=True)
Run Code Online (Sandbox Code Playgroud)

我希望能够以显示由CellCount模型作为的小区属性称为小区的不同machine_name #iddiv.我为CellCount使用ModelFormSet,它将CellType对象列表作为其初始数据传递.

Ala*_*air 5

表单的初始数据存储在其中form.initial,因此请尝试:

{{ form.initial.cell.machine_name }}
Run Code Online (Sandbox Code Playgroud)