Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to import the Django permission model.
Django
from django.contrib.auth.models import [1]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Importing User instead of Permission
Importing Group or Session which are unrelated here
✗ Incorrect
The Permission model is used to manage built-in permissions in Django.
2fill in blank
mediumComplete the code to check if a user has a specific permission.
Django
if user.[1]('app_label.permission_codename'):
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using non-existent methods like check_perm or can
Confusing with group membership checks
✗ Incorrect
The has_perm method checks if a user has a given permission string.
3fill in blank
hardFix the error in the code to assign a permission to a user.
Django
permission = Permission.objects.get(codename='add_article') user.user_permissions.[1](permission)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'assign' which is not a valid method
Using 'remove' which deletes permissions
✗ Incorrect
The add method adds a permission to the user's permissions.
4fill in blank
hardFill both blanks to create a dictionary comprehension of permissions with their names.
Django
{perm.[1]: perm.[2] for perm in Permission.objects.all()} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'id' or 'content_type' as keys or values
Mixing up codename and name
✗ Incorrect
The codename is the key and name is the value for permissions.
5fill in blank
hardFill all three blanks to filter permissions for a specific app and order by name.
Django
permissions = Permission.objects.filter(content_type__app_label=[1]).order_by([2]).values_list([3], flat=True)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong app label string
Ordering by codename instead of name
Getting wrong field in values_list
✗ Incorrect
Filter by app label 'blog', order by 'name', and get list of 'codename's.