如何筛选同一列的多个值

时间:2018-02-21 12:23:24

标签: django django-models

from django.contrib.auth.models import User    
...
resolvers = User.objects.filter(groups__name = 'resolver')

上面的代码是过滤用户属于组解析器,在此我需要检索那些属于管理员组的用户。

我试过

resolvers = User.objects.filter(groups__name = 'resolver' or 'admin')
resolvers = User.objects.filter(groups__name = ('resolver','admin'))

两者都失败了,请帮忙。

1 个答案:

答案 0 :(得分:2)

您可以使用__in

resolvers = User.objects.filter(groups__name__in = ('resolver','admin'))

Q object实施OR条件:

from django.db.models import Q
resolvers = User.objects.filter(Q(groups__name='resolver')| Q(groups__name='admin'))