espenhogbakk espenhogbakk - 1 year ago 92
Python Question

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

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 Source

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()