0
我有3種型號文,評論,網友:燒瓶SQLAlchemy中,選擇使用逆包容性(不)一套
class User(UserMixin, db.Model):
__tablename__ = 'users'
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(64), unique=True, index=True)
posts = db.relationship('Post', backref='author', lazy='dynamic')
comments = db.relationship('Comment', backref='author', lazy='dynamic')
class Post(db.Model):
__tablename__ = 'posts'
id = db.Column(db.Integer, primary_key=True)
body = db.Column(db.Text)
author_id = db.Column(db.Integer, db.ForeignKey('users.id'))
comments = db.relationship('Comment', backref='post', lazy='dynamic')
class Comment(db.Model):
__tablename__ = 'comments'
id = db.Column(db.Integer, primary_key=True)
body = db.Column(db.Text)
author_id = db.Column(db.Integer, db.ForeignKey('users.id'))
post_id = db.Column(db.Integer, db.ForeignKey('posts.id'))
我的問題: 我怎麼可以過濾那些沒有被註釋掉那些帖子由給定用戶(給出user.id)
我有過濾職位那些由給定用戶被註釋的一段代碼:
@property
def commented_posts(self):
return Post.query.join(Comment, Comment.post_id==Post.id).filter(Comment.author_id==self.id)
這不包括尚未被任何* *用戶評論的帖子。 – davidism
這不會起作用,我們應該排除那些由給定用戶評論的帖子集合 –
我已經更新了答案 –