Search code examples
django-permissionsdjango-guardian

How can I get all users an object has specific permissions to in django guardian?


There is

from guardian.shortcuts import get_objects_for_user

But what about

from guardian.shortcuts import get_users_for_object

Thanks.


Solution

  • This is my workaround. This is Model method. You can encapsulate it in mixin.

    def get_users_with_perm(self, permission):
        '''
        Returns list of users(worn:not QuerySet) with specific permission for this object
        :param permission: permission string
        '''
    
        anyperm = get_users_with_perms(self, attach_perms=True)
        result = []
        for user, perms in anyperm.iteritems():
            if permission in perms: result.append(user)
        return result