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


Question

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?

1
57
4/16/2015 2:30:46 AM

Accepted Answer

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()
71
6/9/2016 8:06:35 PM

This would be the easiest

from django.contrib.auth import models

group = models.Group.objects.get(name='blogger')
users = group.user_set.all()

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon