我有点受阻,它似乎不是很复杂。这是我的模型:
class Team(models.Model):
name = models.CharField('team name', max_length=200, unique=True)
class QualityStream(models.Model):
name = models.CharField('quality stream name', max_length=200, unique=True)
team = models.ManyToManyField(Team)
class Milestone(models.Model):
name = models.CharField('milestone name', max_length=200)
quality_stream = models.ForeignKey(QualityStream)
team = models.ForeignKey(Team)
如您所见,一个团队可以有几个预定义的质量流,以及确实属于一个质量流的几个要实现的里程碑。
这是我的看法:
<p>{{team.name}}</p>
{% for stream in team.qualitystream_set.all %}
<p>{{stream.name}}</p>
{% for milestone in team.milestone_set.all %}
{% if milestone.quality_stream.id == stream.id %}
<p>{{milestone.name}}</p>
{% endif %}
{% endfor %}
{% endfor %}
这个想法是显示团队、与团队相关的质量流以及按质量流分组的每个里程碑:
Team
Quality Stream 1
Milestone 1 for Quality Stream 1
Milestone 2 for Quality Stream 1
Quality Stream 2
Milestone 1 for Quality Stream 2
Milestone 2 for Quality Stream 2
代码运行良好,但循环每个质量流的所有里程碑让我感觉不太舒服。我想一定有更好的方法来实现这一点。有任何想法吗?