我试图在我的模型中迭代我的 FK,以便通过各种表显示所有连接。我的模板呈现但不显示任何值。有任何想法吗?
模型.py
class State(models.Model):
state = models.CharField(max_length=255)
relevantdisease = models.ForeignKey(Disease)
relevantoption = models.ManyToManyField(Option, through='StateOption')
class StateOption(models.Model):
partstate = models.ForeignKey(State)
partoption = models.ForeignKey(Option)
relevantoutcome = models.ManyToManyField(Outcome, through='StateOptionOutcome')
class StateOptionOutcome(models.Model):
stateoption = models.ForeignKey(StateOption)
relevantoutcome = models.ForeignKey(Outcome)
outcomevalue = models.CharField(max_length=20)
视图.py
def stateall(request, disease_id):
disease = get_object_or_404(Disease, pk=disease_id)
states = State.objects.select_related().filter(relevantdisease=disease_id)
context = {'disease':disease,'states': states}
return render(request, "stateall.html", context)
模板.html
{% for state in states %}
<li>{% for i in state.stateoption_set.all %}</li>
<li>{% for j in i.stateoptionoutcome_set.all %}</li>
{% endfor %}
{% endfor %}
{% endfor %}
我希望模板显示为:
State1<state>
<li>partoption</li>
<li>relevantoutcome: outcomevalue</li>
State2<state>
<li>partoption</li>
<li>relevantoutcome: outcomevalue</li>
...