Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 594
espenhogbakk Avatar asked Dec 18 '08 16:12

espenhogbakk


People also ask

How do I get user permissions in Django?

If you are using Django 3.0+, user. get_user_permissions() gives the codename of all the permissions. Show activity on this post.

How do you get all groups that a user is a member of Django?

You can get the groups of a user with request. user. groups. all() , which will return a QuerySet .

How can I see active users in Django?

# the password verified for the user if user. is_active: print("User is valid, active and authenticated") else: print("The password is valid, but the account has been disabled!") else: # the authentication system was unable to verify the username and password print("The username and password were incorrect.")


1 Answers

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() 
like image 72
Glader Avatar answered Sep 28 '22 07:09

Glader