如何获得用户权限? [英] How to get user permissions?
本文介绍了如何获得用户权限?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想检索用户的所有权限作为权限ID的列表,但是:
I want to retrieve all permission for user as list of premission id's but:
user.get_all_permissions()
给我权限名称列表。
推荐答案
关键是获取这样的权限对象:
The key is get the permission objects like this:
from django.contrib.auth.models import Permission
permissions = Permission.objects.filter(user=user)
,您可以在其中访问 id
属性,如下所示:
and there you can access the id
property like this:
permissions[0].id
如果要列表(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)]
希望有帮助!
这篇关于如何获得用户权限?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文