6
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.

1 Answer 1

11

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'))
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for your help. 1st approach is working fine. 2nd approach is throwing - 'QuerySet' object is not callable
@user6941347 can you post full traceback?
@user6941347 second should work also. Probably you have variable named Q in your code which overrides django's Q object?
Yes, there was a temp variable in the name of Q. Renamed it, now both approach are working

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.