I have a pretty standard many-to-many relationship, similar to the Blog -> Keyword relationship in the ORM tutorial.
I would like to query for a list of Keywords, returning Blog posts where any of them match. However, I can't work out if there is a simple way to do this. If I add multiple filters, repeatedly doing
.filter(Blog.keywords.any(Keyword.name == 'keyword'))
then I get an 'AND'/'EXISTS' query, such that only posts which have all those keywords would be returned. Is there a simple way to do this as an 'OR' query, or do I need to work using join().
Thanks for any help; I can't work out whether I am missing something.
I think you just want
.filter(Blog.keywords.any(Keyword.name.in_(['keyword1', 'keyword2', ...])))
I'm using http://www.sqlalchemy.org/docs/05/ormtutorial.html#common-filter-operators for reference
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With