Skip to content Skip to sidebar Skip to footer

Django User Does Not Inherit Permission From Group

Consider the following output of a Django shell session: >>> from django.contrib.auth.models import User, Group >>> g=Group.objects.all() >>> g

Solution 1:

Try this to be sure that permission names are correctly constructed:

g = Group.objects.all()
perms = g[1].permissions.all()
us = g[1].user_set.filter(is_active=True)
missingperms = set()
for u in us:
    for p in perms:
        pstring = p.content_type.app_label + '.' + p.codename
        if not u.has_perm(pstring):
            missingperms.add(pstring)
print('missing permissions count:', len(missingperms))

The output should be 0 missing permissions.

Addendum: The fact that a superuser has a specific permission is no proof that the permission is a valid one. You can try this for instance:

u = User.objects.filter(is_superuser=True)[0]
u.has_perm('burn_down_the_house')

Solution 2:

I figured it out. The permission that gets added to the model automatically is called "create_..." but the permission that you add in the admin site is called "add_...". So

>>> u[2].has_perm('testman.add_testplanstep')
True

Strange, though. Doesn't make sense to me to have different names for those.


Post a Comment for "Django User Does Not Inherit Permission From Group"