0
0
Djangoframework~20 mins

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

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
ORM Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Why use ORM in Django?
What is the main reason Django's ORM maps Python classes to database tables?
ATo make the database run faster by converting Python code directly into machine code
BTo allow developers to work with database data using Python objects instead of SQL queries
CTo replace the need for a database by storing all data in Python memory
DTo automatically generate HTML pages from Python classes without templates
Attempts:
2 left
💡 Hint
Think about how ORM helps developers avoid writing raw SQL.
component_behavior
intermediate
2:00remaining
What happens when you save a Django model instance?
Given a Django model instance, what does calling the .save() method do in terms of ORM behavior?
Django
class Book(models.Model):
    title = models.CharField(max_length=100)

book = Book(title='Learn Django')
book.save()
AIt creates or updates a row in the database table corresponding to the Book model
BIt only updates the Python object in memory without affecting the database
CIt deletes the corresponding row from the database
DIt generates an HTML page showing the book details
Attempts:
2 left
💡 Hint
Think about how ORM syncs Python objects with the database.
📝 Syntax
advanced
2:00remaining
Identify the correct Django ORM query syntax
Which option correctly retrieves all Book objects with title 'Django Basics' using Django ORM?
ABook.objects.get_all(title='Django Basics')
BBook.filter(title='Django Basics')
CBook.objects.find(title='Django Basics')
DBook.objects.filter(title='Django Basics')
Attempts:
2 left
💡 Hint
Remember the Django ORM uses 'objects' manager and 'filter' method for queries.
🔧 Debug
advanced
2:00remaining
Why does this Django ORM query raise an error?
What error will this code raise and why? class Author(models.Model): name = models.CharField(max_length=50) # Query: Author.objects.get(name='Unknown')
ATypeError because name field is not a string
BMultipleObjectsReturned error because more than one Author has name 'Unknown'
CDoesNotExist error because no Author with name 'Unknown' exists
DSyntaxError because get() requires a list of names
Attempts:
2 left
💡 Hint
What happens if get() finds no matching record?
state_output
expert
2:00remaining
What is the output of this Django ORM code snippet?
Consider this code: class Product(models.Model): name = models.CharField(max_length=50) price = models.DecimalField(max_digits=5, decimal_places=2) Product.objects.create(name='Pen', price=1.50) Product.objects.create(name='Notebook', price=3.00) cheap_products = Product.objects.filter(price__lt=2.00) print(list(cheap_products.values_list('name', flat=True))) What will be printed?
A['Pen']
B['Pen', 'Notebook']
C[]
D['Notebook']
Attempts:
2 left
💡 Hint
Look at the filter condition price__lt=2.00 and which products match.