[Django]-Django user get_all_permissions() is empty while user_permissions is set

50πŸ‘

βœ…

had the same problem. I am guessing that at some point you have used a self-crafted AUTHENTICATION_BACKEND? Most examples on the net of this (INCLUDING THE DJANGO 1.0 DOCUMENTATION!) don’t mention that the Backends are responsible for permissions handling as well.

However, no biggie: In whatever backend file your code resides, include this import:

from django.contrib.auth.backends import ModelBackend

Then make sure the Backend you wrote extends ModelBackend, e.g.:

class EmailBackend(ModelBackend):

Should be fine.

πŸ‘€Mark Henwood

1πŸ‘

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()
πŸ‘€nezort11

Leave a comment