0

I have two related objects: Post and Comment. One Post can have many Comments. Im using backref so I can reference comment list from post object:

post = Post.query.get(post_id)
all_comments = post.comments # returns all comments of whole post

Now I want to filter those comments by their author. I want to be able to do something like this:

post.comments.filter(author_id=author_id)

In Django I did it like this:

post.comments.all().filter(author_id=author_id)

But in Flask I can't figure it out. I don't to query all database using:

Comment.query.filter_by(post_id=post_id, author_id=author_id)

How do I do that query?

davidism
  • 121,510
  • 29
  • 395
  • 339
ofca1234
  • 53
  • 7

1 Answers1

0

Try this below :

Comment.query.join(Post, Comment.post_id == Post.id).filter_by(Post.post_id=post_id, Comment.author_id=author_id).all()
Abhishek Kulkarni
  • 1,747
  • 1
  • 6
  • 8