在django-guardian中,我如何获得用户拥有任何权限的所有对象

时间:2014-07-27 00:45:23

标签: django django-guardian

在django-guardian中,如何让所有对象用户都有权限(通过组)。有一个shortcut to get all users having any permission over an object,但我正在搜索给定用户拥有任何权限的所有对象。

1 个答案:

答案 0 :(得分:1)

get_objects_for_user()

>>> from guardian.shortcuts import get_objects_for_user
>>> joe = User.objects.get(username='joe')
>>> get_objects_for_user(joe, 'auth.change_group')
[]

>>> from guardian.shortcuts import assign_perm
>>> group = Group.objects.create('some group')
>>> assign_perm('auth.change_group', joe, group)
>>> get_objects_for_user(joe, 'auth.change_group')
[<Group some group>]

对于任何权限,您可以自行在列表中键入代码,或使用get_perms_for_model()

>>> group_perms = [perm.codename for perm in get_perms_for_model(Group)]
>>> get_objects_for_user(joe, group_perms, klass=Group, any_perm=True)
[<Group some group>]