'How to get users from list of IDs in Flask [duplicate]
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)
Solution 1:[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!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | snakecharmerb |
