我有以下型号:
Base = declarative_base()
class Comment(Base):
__tablename__ = "commenting_comment"
id = Column(UUID, primary_key=True, index=True)
text = Column(String)
parent_id = Column(UUID, ForeignKey('commenting_comment.id'))
parent = relationship('Comment', remote_side=[id], backref="replies")
我想数一数
replies
每一条评论。
目前,我有以下疑问:
ids = ["uuid1", "uuid2", "uuid3"]
qs = db.query(
Comment,
func.count('id').label("replies_count"),
).select_from(Comment).group_by(Comment.id).filter(
Comment.id.in_(ids)
).all()
有两个问题:
1.它回来了
replies_count=1
所有的行。
2-我想要
replies_count
在评论的正文中,但它会返回
回复(u count)
在评论栏外。(如下)
comments = [
{"Comment": {...}, "replies_count": 1},
{"Comment": {...}, "replies_count": 1},
]
如何解决这两个问题?
使现代化
在Django中,相应的查询将是:
Comment.objects.filter(id__in=ids).annotate(replies_count=Count("replies"))
但我不知道如何在烧瓶和炼金术中实现它。