Challenge - 5 Problems
Django Permissions Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output of this Django permission check?
Consider a Django view where a user tries to access a resource. The code snippet below checks if the user has the permission 'app.view_resource'. What will be the output if the user does NOT have this permission?
Django
if request.user.has_perm('app.view_resource'): result = 'Access granted' else: result = 'Access denied' print(result)
Attempts:
2 left
💡 Hint
Remember that has_perm returns a boolean indicating if the user has the permission.
✗ Incorrect
The has_perm method returns True if the user has the specified permission, otherwise False. Since the user lacks 'app.view_resource', the else branch runs, printing 'Access denied'.
❓ state_output
intermediate2:00remaining
What is the value of user.get_all_permissions() after adding a group?
Given a user with no permissions initially, you add the user to a group that has the permission 'app.change_item'. What will user.get_all_permissions() return?
Django
group = Group.objects.create(name='Editors') permission = Permission.objects.get(codename='change_item') group.permissions.add(permission) user.groups.add(group) perms = user.get_all_permissions()
Attempts:
2 left
💡 Hint
get_all_permissions returns a set of strings in the format 'app_label.codename'.
✗ Incorrect
When a user is added to a group with permissions, get_all_permissions returns a set including those permissions with the format 'app_label.codename'.
📝 Syntax
advanced2:00remaining
Which option correctly assigns a permission to a user?
You want to assign the permission 'app.delete_post' to a user instance. Which code snippet correctly does this?
Attempts:
2 left
💡 Hint
user_permissions.add expects Permission model instances, not strings.
✗ Incorrect
The add method requires Permission instances. Option A fetches the Permission object by codename and adds it correctly.
🔧 Debug
advanced2:00remaining
Why does this permission check always fail?
A developer writes this code to check if a user has permission to add an object:
if user.has_perm('app.add_model'):
print('Can add')
else:
print('Cannot add')
But it always prints 'Cannot add' even though the user has the permission assigned. What is the likely cause?
Attempts:
2 left
💡 Hint
Check the exact permission codename format Django uses.
✗ Incorrect
Django permission codenames follow the pattern 'action_modelname' where modelname is lowercase and exact. Using 'add_model' instead of 'add_modelname' causes the check to fail.
🧠 Conceptual
expert2:00remaining
Which statement about Django's built-in permission system is TRUE?
Select the correct statement about how Django's built-in permission system works.
Attempts:
2 left
💡 Hint
Think about what Django does automatically when you create models.
✗ Incorrect
Django creates default permissions (add, change, delete, view) for each model automatically. Groups can have permissions, and custom permissions can be added via model Meta class.