Mike
Mike

Reputation: 7831

Django ORM: filter by a list of objects

I have the following code to put all my users from a multichoice field into a list

USERS = []
for user in User.objects.filter(groups__name='accountexec'):
    USERS.append((user.id,user))

I need to use Log.objects.filter() to get all the logs with a user= to a user in the USERS list

Upvotes: 15

Views: 18782

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599490

Use the __in lookup:

Log.objects.filter(user__in=User.objects.filter(groups__name='accountexec'))

Upvotes: 48

Related Questions