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?
If you are using Django 3.0+, user. get_user_permissions() gives the codename of all the permissions. Show activity on this post.
You can get the groups of a user with request. user. groups. all() , which will return a QuerySet .
# 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.")
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()
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