看的是FLASKWEB开发这本书,在自引用关系这一节中:新建了一个FOLLOW表:
class Follow(db.Model):
__tablename__='follows'
follower_id=db.Column(db.Integer,db.ForeignKey('users.id'),primary_key=True)
followed_id=db.Column(db.Integer,db.ForeignKey('users.id'),primary_key=True)
timestamp=db.Column(db.DateTime,default=datetime.utcnow)
然后在User模型中添加属性:
class User(…):
followed=db.relationship('Follow',foreign_keys=[Follow.followed_id],backref=db.backref('follower',lazy='joined'),lazy='dynamic',
cascade='all,delete-orphan')
followers=db.relationship('Follow',foreign_keys=[Follow.follower_id],backref=db.backref('followed',lazy='joined'),lazy='dynamic',
cascade='all,delete-orphan')
在relationship中,backref参数为什么引用的是backref=db.backref(‘followed’,lazy='joined'),而不直接backref='followed'?db.backref(‘followed’,lazy='joined')中个的lazy="joined"参数有什么作用?非常感谢。
添加回答
举报
0/150
提交
取消