27π
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:
permissions[0].id
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!
50π
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)
- [Django]-How to get GET request values in Django?
- [Django]-Remove pk field from django serialized objects
- [Django]-Is APITest with Query params different then just normal url?
12π
If you are using Django 3.0+, user.get_user_permissions()
gives the codename of all the permissions.
More information here: https://docs.djangoproject.com/en/3.0/ref/contrib/auth/#django.contrib.auth.models.User.get_user_permissions
- [Django]-Running django tests with sqlite
- [Django]-How to completely uninstall a Django app?
- [Django]-Access request in django custom template tags
8π
we can get user permission from user objects directly into a list like this
perm_list = user_obj.user_permissions.all().values_list('codename', flat=True)
Try thisβ¦.
- [Django]-Django TemplateDoesNotExist?
- [Django]-Django: How should I store a money value?
- [Django]-Django β run a function every x seconds
5π
This is an routine to query for the Permission objects returned by user.get_all_permissions()
in a single query.
from functools import reduce
from operator import or_
from django.db.models import Q
from django.contrib.auth.models import Permission
def get_user_permission_objects(user):
user_permission_strings = user.get_all_permissions()
if len(user_permission_strings) > 0:
perm_comps = [perm_string.split('.', 1) for perm_string in user_permission_strings]
q_query = reduce(
or_,
[Q(content_type__app_label=app_label) & Q(codename=codename) for app_label, codename in perm_comps]
)
return Permission.objects.filter(q_query)
else:
return Permission.objects.none()
Alternatively, querying Permission directly:
from django.db.models import Q
from django.contrib.auth.models import Permission
def get_user_permission_objects(user):
if user.is_superuser:
return Permission.objects.all()
else:
return Permission.objects.filter(Q(user=user) | Q(group__user=user)).distinct()
- [Django]-Is it OK to use multiple inheritance with Django abstract models?
- [Django]-Django REST Framework: adding additional field to ModelSerializer
- [Django]-How to strip html/javascript from text input in django
1π
from django.contrib.auth.models import Permission
permissions = Permission.objects.filter(user=user)
permissions[0].id
- [Django]-Where to put Django startup code?
- [Django]-Django.db.utils.ProgrammingError: relation "bot_trade" does not exist
- [Django]-Django Model Field Default Based Off Another Field in Same Model
1π
Returns the set of permission strings the user_obj has, including both user permissions and group permissions. Returns an empty set if is_anonymous or is_active is False.
user_permissions_list = request.user.get_all_permissions()
Returns the set of permission strings the user_obj has from the permissions of the groups they belong. Returns an empty set if is_anonymous or is_active is False.
request.user.get_group_permissions()
Returns the set of permission strings the user_obj has from their own user permissions. Returns an empty set if is_anonymous or is_active is False.
request.user.get_user_permissions()
Unfortunately these built in method give me duplicate queries.
Or you can use your own filter from permission tables to get all permissions of current user
user_permissions_list = list(Permission.objects.filter(Q(user=request.user) | Q(group__user=request.user)).values_list('codename', flat=True))
- [Django]-How do you use get_context_data with TemplateView in Django
- [Django]-When to create a new app (with startapp) in Django?
- [Django]-What is choice_set in this Django app tutorial?
0π
Extending and explaining the answer from @Shihabudheen K M
we can get user permission from user objects directly a queryset like this
perm_queryset = user_obj.user_permissions.all().values_list('id')
The queryset obtained in this case is of the following format:
<QuerySet [(31,), (16,), (11,), (35,), (18,), (36,)]>
Flattened queryset of permissions of the user:
flattened_perm_queryset = user_obj.user_permissions.all().values_list('id', flat=True)
Output:
<QuerySet [31, 16, 11, 35, 18, 36]>
To get a list of the ids of these permissions on a user you can do:
perm_list = list(user_obj.user_permissions.all().values_list('id', flat=True))
Output:
[31, 16, 11, 35, 18, 36]
Hope this is helpful!
- [Django]-Change a Django form field to a hidden field
- [Django]-Django "get() got an unexpected keyword argument 'pk'" error
- [Django]-Django multiprocessing and database connections