我的意图是在博客应用程序中获取post.likes.count
。但这由于方法错误而无效。
错误:
'method object' has no attribute 'count'
这是我的代码:
app / templates / post / show.html
<div class="like">
<i class="fas fa-caret-up"></i>
<p>{{ post.likes.count }}</p>
</div>
app / models.py
class Post(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String)
content = db.Column(db.Text)
def likes(self):
likes = db.session.query(PostLike).filter(PostLike.issue_id == self.id)
return likes
class PostLike(db.Model):
id = db.Column(db.Integer, primary_key=True)
user_id = db.Column(db.Integer,db.ForeignKey('users.id'))
post_id = db.Column(db.Integer,db.ForeignKey('posts.id'))
user = db.relationship("User", backref='user_post_like', foreign_keys=[user_id])
post = db.relationship("Post", backref='post_user_like', foreign_keys=[post_id])
def __repr__(self):
return '<PostLike %r>'
发布喜欢的结果:
(Pdb) post.likes
<bound method Post.likes of <Post 'xxxxxxxx'>
答案 0 :(得分:1)
来自:
<div class="like">
<i class="fas fa-caret-up"></i>
<p>{{ post.likes.count }}</p>
</div>
收件人:
<div class="like">
<i class="fas fa-caret-up"></i>
<p>{{ post.likes().count() }}</p>
</div>