Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

filter with more than one value on flask-sqlalchemy [duplicate]

I want a query with multiple value like where id in (3,4,5)

Then I have a my_list = [3,4,5] so how can I pass that list as an argument to filter in sqlalchemy?

query = Notification.query.filter_by(id=my_list).all()
like image 351
Ricardo Avatar asked Jan 03 '15 23:01

Ricardo


2 Answers

Use in_ with filter:

query = Notification.query.filter(Notification.id.in_(my_list)).all()

Here's the relevant SO Q&A, and Read the Doc from sqlalchemy

like image 179
Anzel Avatar answered Sep 28 '22 01:09

Anzel


In case Notification has a JSON field e.g. data, is there similar syntax to check a list e.g:

query = Notification.query.filter(Notification.data['key'].in_(my_list)).all()
like image 20
ruben Avatar answered Sep 28 '22 02:09

ruben