如何在django中显示评论数量

时间:2016-03-11 13:25:41

标签: django

我有点困惑,我有两个模特

class Comment(models.Model):
    user = models.ForeignKey(MyProfile)
    parent = models.ForeignKey("self", null=True, blank=True)
    path = models.CharField(max_length=350)
    post = models.ForeignKey(Post, null=True, blank=True, related_name="commented_post")
    text = models.TextField()

并且

class Post(models.Model):
    #comment = models.ForeignKey(Comment)if I do this I get an error but how do I display the number of comments? I was thinking to do {{post.comment.count}}

我尝试在评论模型

下添加以下功能
@property
    def get_comment_count(self):
        return self.comment.count

但是这不起作用,我想我必须使用像{{comment.post.count}}这样的东西......任何帮助请...

1 个答案:

答案 0 :(得分:-1)

看起来您正在尝试获取特定帖子的评论数量,您可以comments_count = Comment.objects.filter(post=mypost).count()