tags:

views:

951

answers:

1

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

+4  A: 

Use the __in lookup:

Log.objects.filter(user__in=User.objects.filter(groups__name='accountexec'))
Daniel Roseman
Actually, just "Log.objects.filter(user__in=User.objects.filter(groups__name='accountexec'))". Django will turn that into a single query + subquery.
James Bennett
Took the liberty of improving the suggested query according to James' suggestion, hope that's OK.
Carl Meyer