Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to filter multiple values for same column

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

above code is to filter user belongs to group resolver, in this I need to retrieve users those belongs to admin group as well.

I tried

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

both are failing, please help.

like image 280
Rohit Shekhar Avatar asked Feb 21 '18 12:02

Rohit Shekhar


People also ask

How do I filter multiple values in the same column in R?

In this, first, pass your dataframe object to the filter function, then in the condition parameter write the column name in which you want to filter multiple values then put the %in% operator, and then pass a vector containing all the string values which you want in the result.


Video Answer


1 Answers

You can use __in:

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

or Q object to implement OR condition:

from django.db.models import Q
resolvers = User.objects.filter(Q(groups__name='resolver')| Q(groups__name='admin'))
like image 128
neverwalkaloner Avatar answered Nov 03 '22 01:11

neverwalkaloner