0
0
Djangoframework~20 mins

Group-based permissions in Django - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Group Permission Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this Django group permission check?
Consider a Django view where a user belongs to a group named 'editors'. The view checks if the user has permission 'blog.change_post' via group membership. What will be the output of the permission check if the group 'editors' has the 'change_post' permission assigned?
Django
from django.contrib.auth.models import Group
user = request.user
has_perm = user.has_perm('blog.change_post')
print(has_perm)
ARaises AttributeError
BFalse
CTrue
DNone
Attempts:
2 left
💡 Hint
Remember that Django checks user permissions including those granted by groups.
📝 Syntax
intermediate
2:00remaining
Identify the correct way to add a user to a group in Django
Which of the following code snippets correctly adds a user to a group named 'managers'?
A
group = Group.objects.get(name='managers')
user.groups.add(group)
Buser.add_group('managers')
CGroup.add_user(user, 'managers')
Duser.groups.append('managers')
Attempts:
2 left
💡 Hint
Check the Django documentation for the correct method to add a group to a user's groups.
🔧 Debug
advanced
2:00remaining
Why does this permission check always return False?
A developer writes this code to check if a user has permission 'app.delete_item'. The user belongs to a group with this permission, but the check returns False. What is the likely cause?
Django
if request.user.has_perm('app.delete_item'):
    print('Permission granted')
else:
    print('Permission denied')
AUser's group permissions are not automatically checked by has_perm
BThe permission codename is incorrect or missing app label
CThe user is not authenticated
DThe permission is assigned to the user directly, not the group
Attempts:
2 left
💡 Hint
Check the exact permission string format: it must be '.'.
state_output
advanced
2:00remaining
What is the number of permissions a user has after adding two groups with overlapping permissions?
A user belongs to two groups: 'writers' and 'editors'. Both groups have the permission 'blog.add_post'. The 'writers' group also has 'blog.view_post'. How many unique permissions does the user have?
Django
from django.contrib.auth.models import User
user = User.objects.get(username='alice')
permissions = user.get_all_permissions()
print(len(permissions))
A4
B3
C1
D2
Attempts:
2 left
💡 Hint
Remember that permissions are unique and duplicates from groups count only once.
🧠 Conceptual
expert
2:00remaining
Which statement best describes Django's group-based permission inheritance?
Select the statement that correctly explains how Django handles permissions for users assigned to groups.
AUsers inherit all permissions assigned to their groups automatically when checking permissions.
BPermissions assigned to groups override user permissions and cannot be combined.
CGroup permissions are ignored unless explicitly checked with group.has_perm().
DUsers must manually copy group permissions to their user permissions to inherit them.
Attempts:
2 left
💡 Hint
Think about how Django's has_perm method works with groups.