Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get all users of a group in Django?

Tags:

django

I want to get a list of all users who are within a Django Group. For example:

User.objects.filter(group='Staff')

I cannot find how to do this query anywhere in the docs.

like image 307
MiniGunnR Avatar asked Sep 20 '16 06:09

MiniGunnR


People also ask

How do I get all users in Python?

PROGRAM. First, import the psutil Python Library to access the users() method. Now, use the method psutil. users() method to get the list of users as a named tuple and assign to the variable user_list.

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.

WHAT IS group in Django?

Django does provide groups and permissions option but this is a model or table level and not at the object level. Hence we decided on creating groups based on the objects on which we want to provide the access and the users were added to these groups as per requirement or based on the existing state of an object.


1 Answers

The following query solved my problem.

User.objects.filter(groups__name='Staff')

Thanks to @SardorbekImomaliev for figuring it out.

like image 176
MiniGunnR Avatar answered Sep 29 '22 08:09

MiniGunnR