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 users with a specific permission group, something like this:

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

I can’t find out how to do this. How are permission groups stored in the user model?

+45
python dictionary django permissions
Dec 18 '08 at 16:00
source share
9 answers

If you want a list of users by permission, look at this option:

 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() 
+57
Jun 14 '09 at 7:02
source share

It will be the easiest

 from django.contrib.auth import models group = models.Group.objects.get(name='blogger') users = group.user_set.all() 
+40
Dec 18 '08 at 19:03
source share

I think that for group permissions, permissions are stored in groups, and then users have groups associated with them. This way you can simply resolve the relationship of user groups.

eg.

 518$ python manage.py shell (InteractiveConsole) >>> from django.contrib.auth.models import User, Group >>> User.objects.filter(groups__name='monkeys') [<User: cms>, <User: dewey>] 
+16
Dec 18 '08 at 19:11
source share

Based on @Glader's answer, this function wraps it in a single request and has been modified so that algo gets superusers (since by definition they have all the perms):

 from django.contrib.auth.models import User from django.db.models import Q def users_with_perm(perm_name): return User.objects.filter( Q(is_superuser=True) | Q(user_permissions__codename=perm_name) | Q(groups__permissions__codename=perm_name)).distinct() # Example: queryset = users_with_perm('blogger') 
+7
Sep 05 '13 at 14:11
source share

Remember that specifying a code name is not allowed enough, because different applications can reuse the same name. You need to get a permission object to correctly request users:

 def get_permission_object(permission_str): app_label, codename = permission_str.split('.') return Permission.objects.filter(content_type__app_label=app_label, codename=codename).first() def get_users_with_permission(permission_str, include_su=True): permission_obj = get_permission_object(permission_str) q = Q(groups__permissions=permission_obj) | Q(user_permissions=permission_obj) if include_su: q |= Q(is_superuser=True) return User.objects.filter(q).distinct() 

Import code: https://github.com/Dmitri-Sintsov/django-jinja-knockout/blob/master/django_jinja_knockout/models.py

+2
Jun 09 '16 at 9:53 on
source share

Groups are many to many with users (you see, nothing unusual, just Django models ...), so the answer to cms is right. Plus it works in both directions: having a group, you can list all the users in it by checking the user_set attribute.

+1
Dec 22 '08 at 14:34
source share

Try the following:

 User.objects.filter(groups__permissions = Permission.objects.get(codename='blogger')) 
0
Jan 19 '09 at 10:02
source share

Based on @Augusto's answer, I did the following with the model manager and using the authtools library. This is in querysets.py :

 from django.db.models import Q from authtools.models import UserManager as AuthUserManager class UserManager(AuthUserManager): def get_users_with_perm(self, perm_name): return self.filter( Q(user_permissions__codename=perm_name) | Q(groups__permissions__codename=perm_name)).distinct() 

And then in models.py :

 from django.db import models from authtools.models import AbstractEmailUser from .querysets import UserManager class User(AbstractEmailUser): objects = UserManager() 
0
Jan 13 '16 at 22:21
source share
 $ python manage.py shell <<'EOF' > from django.contrib.auth.models import User > User.objects.filter(groups__name='blogger') > EOF ... (InteractiveConsole) >>> >>> [<User: foo>, <User: bar>, <User: baz>, '...(remaining elements truncated)...'] 

(simplified from cms answer which I cannot edit)

0
May 25 '17 at 17:00
source share



All Articles