0
0
Djangoframework~10 mins

Why ORM maps Python to database in Django - Test Your Understanding

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

Complete the code to define a Django model representing a simple Book with a title.

Django
from django.db import models

class Book(models.Model):
    title = models.[1](max_length=100)
Drag options to blanks, or click blank then click option'
ADateField
BIntegerField
CCharField
DBooleanField
Attempts:
3 left
💡 Hint
Common Mistakes
Using IntegerField for text data
Forgetting to specify max_length for CharField
2fill in blank
medium

Complete the code to query all Book objects from the database using Django ORM.

Django
books = Book.[1].all()
Drag options to blanks, or click blank then click option'
Aobjects
Bqueryset
Cfilter
Dget
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'filter' without arguments
Using 'get' which returns a single object
3fill in blank
hard

Fix the error in the code to save a new Book instance to the database.

Django
book = Book(title='Django Basics')
book.[1]()
Drag options to blanks, or click blank then click option'
Ainsert
Bcreate
Cadd
Dsave
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'create' on an instance instead of the manager
Using 'add' which is for many-to-many relations
4fill in blank
hard

Fill both blanks to filter Books with titles containing 'Django' and order them by title.

Django
books = Book.objects.[1](title__icontains='Django').[2]('title')
Drag options to blanks, or click blank then click option'
Afilter
Border_by
Cexclude
Dall
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'exclude' instead of 'filter'
Using 'all' which returns all records without filtering
5fill in blank
hard

Fill all three blanks to create a dictionary mapping book titles to their IDs for books published after 2020.

Django
books_dict = {book.[1]: book.[2] for book in Book.objects.[3](published_year__gt=2020)}
Drag options to blanks, or click blank then click option'
Atitle
Bid
Cfilter
Dall
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'all' instead of 'filter' to limit books
Swapping title and id in the dictionary keys and values