user2990084
user2990084

Reputation: 2840

Find each user object for list of user ids

I have a list of user ids, then i need to get the user object for each id. So, I am using this code, that works correctly, but probably there is a better way.

Now my question is about sql alchemy. There is any way to do this without an iteration for each user id? Something like pass the list and then use the all() method.

This is possible?

Here is the code:

    following_users_list_data = []  

    for key in following_users:
        following_users_list_data.append(User.query.filter_by(id=int(key)).first())

    #list of objects   
    print following_users_list_data

Upvotes: 0

Views: 193

Answers (1)

tipu
tipu

Reputation: 9604

something like this would work:

following_users_list_data = User.query.filter(User.id.in_(following_users)).all()

Upvotes: 3

Related Questions