Django ORM:按对象列表过滤

时间:2021-10-20 01:34:46

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

我有以下代码将我的所有用户从multichoice字段放入列表

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

我需要使用Log.objects.filter()将所有带有user =的日志输出到USERS列表中的用户

1 个解决方案

#1


47  

Use the __in lookup:

使用__in查找:

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

#1


47  

Use the __in lookup:

使用__in查找:

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