0
0
Djangoframework~10 mins

Group-based permissions 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 Group model from Django's auth module.

Django
from django.contrib.auth.models import [1]
Drag options to blanks, or click blank then click option'
AUser
BPermission
CGroup
DAdmin
Attempts:
3 left
💡 Hint
Common Mistakes
Importing User instead of Group
Using incorrect module for Group
Misspelling the model name
2fill in blank
medium

Complete the code to create a new group named 'Editors'.

Django
new_group = Group.objects.[1](name='Editors')
Drag options to blanks, or click blank then click option'
Acreate
Bget
Cfilter
Dall
Attempts:
3 left
💡 Hint
Common Mistakes
Using filter which returns a queryset
Using get which retrieves existing objects
Using all which returns all objects
3fill in blank
hard

Fix the error in the code to add a user to a group.

Django
user.groups.[1](group)
Drag options to blanks, or click blank then click option'
Aremove
Badd
Cinsert
Dappend
Attempts:
3 left
💡 Hint
Common Mistakes
Using append which is not a Django queryset method
Using remove which deletes a relation
Using insert which is not valid here
4fill in blank
hard

Fill both blanks to check if a user belongs to the 'Admins' group.

Django
if user.groups.filter(name=[1]).[2]():
    print('User is an admin')
Drag options to blanks, or click blank then click option'
A'Admins'
Bexists
Ccount
D'Editors'
Attempts:
3 left
💡 Hint
Common Mistakes
Using count() which returns a number instead of a boolean
Using wrong group name string
Omitting quotes around the group name
5fill in blank
hard

Fill all three blanks to assign the 'change_article' permission to the 'Editors' group.

Django
permission = Permission.objects.get(codename=[1])
group = Group.objects.get(name=[2])
group.permissions.[3](permission)
Drag options to blanks, or click blank then click option'
A'change_article'
B'Editors'
Cadd
Dremove
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong codename string
Using wrong group name string
Using remove instead of add