我有一个带有一组初始数据的 Django 表单集,它将外键关系对象加载到初始表单中:
{{ cellcountformset.management_form }}
{% for form in cellcountformset %}
<div id="">
{{ form.errors }}
{{ form.as_p }}
</div>
{% endfor %}
相关模型如下所示:
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)
我希望能够将 CellCount 模型的单元格属性所引用的单元格的 machine_name 显示#id
为div
. 我为 CellCount 使用了 ModelFormSet,它传递了一个 CellType 对象列表作为其初始数据。