我有以下型号:
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
archived = db.Column(db.Boolean, default=False)
class Post(db.Model):
id = db.Column(db.Integer, primary_key=True)
content = db.Column(db.UnicodeText, nullable=True)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False, index=True)
user = db.relationship(
'User',
backref='posts',
foreign_keys='Post.user_id',
)
如何在Post
关系列的user
上添加过滤器,以过滤掉已归档的用户?我想避免将关系类型更改为lazy='dynamic'
,而是在模型中将其更改为
答案 0 :(得分:0)
您可以在Post:
下设置属性@property
def unfiltered_posts():
return SESSION.query(cls).Join(User).filter(User.archived==False).all()
答案 1 :(得分:0)
User.query.filter(/**condition/)
吸引用户Post.query.with_parent(user).filter(/**condition/)
喜欢:
user = User.query.filter_by(id='')
posts = Post.query.with_parent(user).filter(Post.id > 3).all()