1

我有一个三层“深度”模型,我想通过我的模板访问它:

楷模:

class Match(models.Model):

    pl1 = models.IntegerField(default=0)
    pl2 = models.IntegerField(default=0)
    boards = models.ManyToManyField(Scoreboard)
    active = models.IntegerField(default=1)
    turn = models.IntegerField(default=0)

    def __unicode__(self):
        return "Match " + str(self.id)

class Scoreboard(models.Model):

    user = models.ForeignKey(User)

我只是在猜测,但要获得连接到每个“匹配”的用户名称,不应该是这样的:

{% for active in matches %}
{{active}} - {{active.boards.user}}<br>
{% endfor %}
4

1 回答 1

2

boards是ManyToMany,所以每个都有很多记分牌match。因此,您需要遍历它们。

{% for active in matches %}
  {{ active }}:
  {% for board in active.boards.all %}
      {{ board.user }}
  {% endfor %}
{% endfor %}
于 2012-07-16T00:34:51.773 回答