How to get a list of all users with a specific permission group in Django

espenhogbakk picture espenhogbakk · Dec 18, 2008 · Viewed 59.6k times · Source

I want to get a list of all Django auth user with a specific permission group, something like this:

user_dict = {
    'queryset': User.objects.filter(permisson='blogger')
}

I cannot find out how to do this. How are the permissions groups saved in the user model?

Answer

Glader picture Glader · Jun 14, 2009

If you want to get list of users by permission, look at this variant:

from django.contrib.auth.models import User, Permission
from django.db.models import Q

perm = Permission.objects.get(codename='blogger')  
users = User.objects.filter(Q(groups__permissions=perm) | Q(user_permissions=perm)).distinct()