18

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()
Ilja Everilä
  • 50,538
  • 7
  • 126
  • 127
Ricardo
  • 7,921
  • 14
  • 64
  • 111

2 Answers2

41

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

Community
  • 1
  • 1
Anzel
  • 19,825
  • 5
  • 51
  • 52
2

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()
David
  • 11,245
  • 3
  • 41
  • 46
ruben
  • 21
  • 1