有没有办法将模型的行循环到表中。如果我可以排除某些字段,它也会非常有用。我正在制作一个表单,用户可以通过在一个模型中创建自己要问的问题和一个答案模型来创建
例如:
模型.py
class Questions(models.Model):
name = models.Charfield()
Question1 = models.Charfield()
Question2 = models.Charfield()
ect
class Answers(models.Model):
question = models.ForeignKey(Questions, related_name='question')
qustion_no = models.IntegerField()
answer = models.Charfield()
表单.html
<table>
<tr>
<th>Question</th>
<th>Answers</th>
</tr>
{% for q in Questions %}
<tr>
<td>{{q}}</td>
<td>{{q.question}}</td
</tr>
{% endfor %}
</table>
请您参考如下方法:
我认为您可能正在搜索类似的内容。
<table>
<tr>
<th>Question</th>
<th>Answers</th>
</tr>
{% for q in Questions %}
<tr>
{% for a in q.question.all %}
<td>{{q}}</td>
<td>{{a.answer}}</td>
{% endfor %}
</tr>
{% endfor %}
</table>
