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)
Hope for this help
Charlesthk Nov 21 '13 at 0:45 2013-11-21 00:45
source share