In Django, how to check if a user is in a specific group?

I created a user group on the Django admin site.

In my code, I want to check if the user is in this group. How to do it?

+127
python django django-authentication
Jan 25 2018-11-11T00:
source share
10 answers

You can access groups simply through the groups attribute in User .

 from django.contrib.auth.models import User, Group group = Group(name = "Editor") group.save() # save this new group for this example user = User.objects.get(pk = 1) # assuming, there is one initial user user.groups.add(group) # user is now in the "Editor" group 

then user.groups.all() returns [<Group: Editor>] .

Alternatively, or rather, you can check if the user is in the group:

 if django_user.groups.filter(name = groupname).exists(): ... 

Note that the groupname may also be the actual Django Group object.

+91
Jan 25 '11 at 1:11
source share

The User object is associated with the Group using the ManyToMany relationship.

That way you can apply the filter method to user.groups .

So, to check if a given User is in a specific group ("Member" for example), simply do the following:

 def is_member(user): return user.groups.filter(name='Member').exists() 

If you want to check whether a given user belongs to several specific groups, use the __ operator in , for example:

 def is_in_multiple_groups(user): return user.groups.filter(name__in=['group1', 'group2']).exists() 

Please note that these functions can be used with the @user_passes_test decorator to control access to your views:

 from django.contrib.auth.decorators import login_required, user_passes_test @login_required @user_passes_test(is_member) # or @user_passes_test(is_in_multiple_groups) def myview(request): # Do your processing 

Hope for this help

+184
Nov 21 '13 at 0:45
source share

If you do not need a user instance on the site (like me), you can do this with

 User.objects.filter(pk=userId, groups__name='Editor').exists() 

This will create only one query to the database and return a boolean value.

+15
Aug 21 '15 at 8:58
source share

If you need a list of users in a group, you can do this instead:

 from django.contrib.auth.models import Group users_in_group = Group.objects.get(name="group name").user_set.all() 

and then check

  if user in users_in_group: # do something 

to check if the user is in a group.

+14
Feb 22 '13 at 17:30
source share

You just need one line:

 from django.contrib.auth.decorators import user_passes_test @user_passes_test(lambda u: u.groups.filter(name='companyGroup').exists()) def you_view(): return HttpResponse("Since you're logged in, you can see this text!") 
+10
Jan 26 '15 at 18:47
source share

If the user belongs to a specific group or not, you can check in django templates using:

{% if group in request.user.groups.all %} "some action" {% endif %}

+10
Aug 25 '15 at 6:57
source share

Just in case, if you want to check that a user group belongs to a predefined list of groups:

 def is_allowed(user): allowed_group = set(['admin', 'lead', 'manager']) usr = User.objects.get(username=user) groups = [ x.name for x in usr.groups.all()] if allowed_group.intersection(set(groups)): return True return False 
+1
Oct 13 '13 at 12:33 on
source share

In one line:

 'Groupname' in user.groups.values_list('name', flat=True) 

This evaluates to True or False .

0
Nov 05 '14 at 12:18
source share

I did it as follows. It seems ineffective, but I had no other way in my head:

 @login_required def list_track(request): usergroup = request.user.groups.values_list('name', flat=True).first() if usergroup in 'appAdmin': tracks = QuestionTrack.objects.order_by('pk') return render(request, 'cmit/appadmin/list_track.html', {'tracks': tracks}) else: return HttpResponseRedirect('/cmit/loggedin') 
0
Apr 02 '18 at 18:16
source share

User.objects.filter (username = 'tom', groups__name = 'admin'). Exist ()

This request will tell you the user: "tom" whether or not the admin group belongs to you

-one
May 07 '18 at 9:52
source share



All Articles