0
0
Djangoframework~10 mins

get() for single objects 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 get a single Book object by its id.

Django
book = Book.objects.[1](id=5)
Drag options to blanks, or click blank then click option'
Aget
Bfilter
Call
Dexclude
Attempts:
3 left
💡 Hint
Common Mistakes
Using filter() instead of get(), which returns a queryset, not a single object.
Using all() which returns all objects, not filtered by id.
2fill in blank
medium

Complete the code to get a User object by username.

Django
user = User.objects.[1](username='alice')
Drag options to blanks, or click blank then click option'
Aget
Ball
Cexclude
Dfilter
Attempts:
3 left
💡 Hint
Common Mistakes
Using filter() which returns a queryset, not a single object.
Using all() which returns all users.
3fill in blank
hard

Fix the error in the code to get a Product by its sku.

Django
product = Product.objects.[1](sku='12345')
Drag options to blanks, or click blank then click option'
Aexclude
Bfilter
Call
Dget
Attempts:
3 left
💡 Hint
Common Mistakes
Using filter() and then trying to access attributes directly without indexing.
Using all() which returns all products.
4fill in blank
hard

Fill both blanks to get a Customer by email and handle the case if not found.

Django
try:
    customer = Customer.objects.[1](email=[2])
except Customer.DoesNotExist:
    customer = None
Drag options to blanks, or click blank then click option'
Aget
B'user@example.com'
C'customer@example.com'
Dfilter
Attempts:
3 left
💡 Hint
Common Mistakes
Using filter() which returns a queryset, not a single object.
Not quoting the email string.
5fill in blank
hard

Fill all three blanks to get an Order by order_id, check if it exists, and assign None if not.

Django
try:
    order = Order.objects.[1]([2]=[3])
except Order.DoesNotExist:
    order = None
Drag options to blanks, or click blank then click option'
Aget
Border_id
C42
Dfilter
Attempts:
3 left
💡 Hint
Common Mistakes
Using filter() which returns a queryset, not a single object.
Using wrong field name or value.