0
0
Djangoframework~10 mins

Built-in permission system in Django - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
AUser
BGroup
CSession
DPermission
Attempts:
3 left
💡 Hint
Common Mistakes
Importing User instead of Permission
Importing Group or Session which are unrelated here
2fill in blank
medium

Complete 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'
Acan
Bcheck_perm
Chas_perm
Dis_allowed
Attempts:
3 left
💡 Hint
Common Mistakes
Using non-existent methods like check_perm or can
Confusing with group membership checks
3fill in blank
hard

Fix 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'
Aassign
Badd
Cremove
Dset
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'assign' which is not a valid method
Using 'remove' which deletes permissions
4fill in blank
hard

Fill 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'
Acodename
Bname
Cid
Dcontent_type
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'id' or 'content_type' as keys or values
Mixing up codename and name
5fill in blank
hard

Fill 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'
A'blog'
B'name'
C'codename'
D'id'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong app label string
Ordering by codename instead of name
Getting wrong field in values_list