tag (auth)

archive

get all users in a group in django May
10
1
0

There's a couple of ways to get the list of all users in a group in Django.

First, you should include the User and Groups class from django.contrib.auth (I'm going to assume you have this included in INSTALLED_APPS or else you wouldn't be here).

from django.contrib.auth.models import User, Group

Then you can do:

group = Group.objects.get(name='blogger')
users = group.user_set.all()

or

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

or if you want to get fancy

perm = Permission.objects.get(codename='blogger')
users = User.objects.filter(Q(groups__permissions=perm) | Q(user_permissions=perm) ).distinct()

comments