from django.contrib.auth.models import Group user = request.user has_perm = user.has_perm('blog.change_post') print(has_perm)
If the user belongs to a group that has the 'change_post' permission, user.has_perm('blog.change_post') returns True. Django automatically includes group permissions in this check.
In Django, to add a user to a group, you first get the group object and then use user.groups.add(group). The other options use invalid methods or attributes.
if request.user.has_perm('app.delete_item'): print('Permission granted') else: print('Permission denied')
If the permission string is incorrect or missing the app label, has_perm will always return False even if the user belongs to a group with the correct permission.
from django.contrib.auth.models import User user = User.objects.get(username='alice') permissions = user.get_all_permissions() print(len(permissions))
The user has two unique permissions: 'blog.add_post' (from both groups, counted once) and 'blog.view_post' (from 'writers'). So total is 2.
Django automatically includes permissions from all groups a user belongs to when checking user.has_perm(). No manual copying or separate checks are needed.