2015-09-08 110 views
0

我有2個模型:帖子和評論,一個帖子有很多評論,我如何過濾comments.count()小於2的帖子?flask sqlalchemy查詢過濾器關係

Post: id(primary key), body, author_id, comments 
Comment: id(primary key), body, author_id, post_id 

其中,

comments = db.relationship('Comment', backref='post', lazy='dynamic') 

非常感謝你!

回答

0

我不認爲你可以找到比掃描整個表更好的解決方案。

爲了提高效率,您可以在帖子表中添加一個字段(帶索引)num_comments,並在每次發表新評論時對其進行更新。