0

I am trying to create variable of users whose ID is in list but i dont know how. This is what I tried but it doesnt work. Does someone know how to filter it correctly?

list = [1, 2, 3, 4]
users = Users.query.filter(Users.id in list)
  • Flask uses sqlalchemy by default, so this [SQLAlchemy IN clause](https://stackoverflow.com/questions/8603088/sqlalchemy-in-clause) must answer your question – sudden_appearance Apr 02 '22 at 22:20

1 Answers1

-1

try this instead

list = [1, 2, 3, 4]
all = Users.query.all() # get all users
users = [] # in the end, this list will store the users which have an id in the list

# loop through all the users
for user in all:
  if user.id in list:
    users.append(user) # if the user's id is in list append to list

# you now have a list of users in list stored in the variable users!
snakecharmerb
  • 47,570
  • 11
  • 100
  • 153