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.
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.
If you are using Django 3.0+, user. get_user_permissions() gives the codename of all the permissions.
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.
The following query solved my problem.
User.objects.filter(groups__name='Staff')
Thanks to @SardorbekImomaliev for figuring it out.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With