[Answered ]-In django-guardian, how can I get all objects user has any permissions for

2👍

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>]

For any permission you could type the codenames in a list yourself, or use 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>]

Leave a comment