我有以下模型设置:
class Link(models.Model):
votes = LinkVoteManager()
class Vote(models.Model):
voter = models.ForeignKey(User)
link = models.ForeignKey(Link)
然而;我无法获得link.votes来显示任何东西,它只是空的。有任何想法吗?我完全卡住了。
谢谢!
答案 0 :(得分:0)
为此目的,您不需要自定义管理器:
只是做
class Link(models.Model):
titlename = models.CharField(max_length=100)
class Vote(models.Model):
voter = models.ForeignKey(User)
link = models.ForeignKey(Link, related_name="link_votes")
然后获得链接的投票做这个:
{% for link in link_list %}
{{link.titlename}}'s voters:
{# here, we are showing all voters for a given link: #}
{% for vote in link.link_votes.all %}
voter's name: {{ vote.voter.username }}
{% endfor %}
{% endfor %}