I want to retrieve all permission for user as list of premission id's but:
give me list of permission names. How to do it?
The key is get the permission objects like this:
from django.contrib.auth.models import Permission permissions = Permission.objects.filter(user=user)
and there you can access the
id property like this:
If you want the list
(id, permission_name) do the following:
perm_tuple = [(x.id, x.name) for x in Permission.objects.filter(user=user)]
Hope it helps!
to get all the permissions of a given user, also the permissions associated with a group this user is part of:
from django.contrib.auth.models import Permission def get_user_permissions(user): if user.is_superuser: return Permission.objects.all() return user.user_permissions.all() | Permission.objects.filter(group__user=user)