sqlalchemy, select using reverse-inclusive (not in) list of child column values

社会主义新天地 提交于 2019-12-02 22:29:36

Pretty straightforward using negated any:

query = session.query(Post).filter(~Post.tags.any(Tag.name.in_(['dont', 'want', 'these'])))

Try this one, easy:

users = session.query(Post).filter(not_(Post.tags.name.in_(['dont', 'want', these'])))

Hope this helps!

I thought up a nasty solution, but it works for the time being. I'd be interested to hear if anyone comes up with a smarter method.

ignore_ids = [item.post_id for item in Tag.query.filter(Tag.name.in_(['dont','want','these'])).all()]
Post.query.filter(Post.id.notin_(ignore_ids))

The notin_ works for me, adjusted example:

db.session.query(Post).filter(Post.tags.notin_(['dont','want','these'])) 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!