假设我有以下模型:
class Holder(models.Model):
other_field = models.BooleanField(default=True)
object_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
object_id = models.PositiveIntegerField()
object = GenericForeignKey('object_type', 'object_id')
然后在 views.py 中,我为 Holder 创建了一个表单集,并将其传递给模板:
formset = modelformset_factory(Holder, fields =('other_field',))
data = {'formset': formset}
在template.html中,我想访问相关对象的一个字段。我尝试过:
{% for form in formset %}
{{ form.object.related_field }}
{{ form.other_field }}
{% endfor %}
然后,显示other_field但不显示related_field。如何在模板中显示相关字段中的值?