Django user get_all_permissions() is empty while user_permissions is set

Viewed 12473

I added some permissions to a user via the admin interface.

From some reason all the perm functions fail, e.g

>>> user.get_all_permissions()
set([])

But accessing the table directly, works:

>>> user.user_permissions.all()
(list of permissions as expected)

What can cause the "get_all_permissions" (and all the perm functions like has_perm()) to fail ?

Thanks

2 Answers

In my case it was because of permission caching. I get the user, added permission to user.user_permissions but user.get_all_permissions was empty set() and user.has_perm was False. This problem is only with shell not admin.

user = User.objects.get(username="User")

permission = Permission.objects.get(
    codename="organizations.add_organization",
)
user.user_permissions.add(permission)

user.get_all_permissions()  # set()
user.has_perm('organizations.add_organization')  # False

I have to add additional line before checking permissions:

user.user_permissions.add(permission)

user = User.objects.get(username="User") # new

user.get_all_permissions()
Related