在django-guardian中,如何让所有对象用户都有权限(通过组)。有一个shortcut to get all users having any permission over an object,但我正在搜索给定用户拥有任何权限的所有对象。
答案 0 :(得分:1)
>>> 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>]