Followers
对于多对多,通常需要一张关联表,这张关联表有两个属性,分别是表1的ID和表2的ID
原则上,从视图函数中移除应用程序的逻辑到数据模型中是一种好的方式。你们必须要保证视图函数尽可能简单,因为它能难被自动化测试。
Post.query.join(followers,
(followers.c.followed_id == Post.user_id))#将Post和followers表连接,连接条件为:(followers.c.followed_id == Post.user_id)
class User(UserMixin, db.Model):
followed = db.relationship(##将user与其它user链接起来,left side user
'User', secondary=followers,
primaryjoin=(followers.c.follower_id == id),
secondaryjoin=(followers.c.followed_id == id),
backref=db.backref('followers', lazy='dynamic'), lazy='dynamic')
代码自动化测试User model(unittest)

浙公网安备 33010602011771号