0
0
Djangoframework~10 mins

Ordering and slicing querysets 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 order the queryset by the 'name' field in ascending order.

Django
products = Product.objects.all().order_by([1])
Drag options to blanks, or click blank then click option'
A'-name'
B'name'
C'price'
D'-price'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-name' which sorts descending instead of ascending.
Using a field name that does not exist in the model.
2fill in blank
medium

Complete the code to get the first 5 items from the queryset.

Django
top_products = Product.objects.all().order_by('rating')[1]
Drag options to blanks, or click blank then click option'
A[:5]
B[:10]
C[5:]
D[1:5]
Attempts:
3 left
💡 Hint
Common Mistakes
Using [5:] which skips the first 5 items instead of taking them.
Using [:10] which returns 10 items instead of 5.
3fill in blank
hard

Fix the error in the code to order by 'created_at' descending.

Django
recent_items = Item.objects.all().order_by([1])
Drag options to blanks, or click blank then click option'
A-created_at
B'created_at'
Ccreated_at
D'-created_at'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the field name without quotes causing a NameError.
Using 'created_at' without minus sign which orders ascending.
4fill in blank
hard

Fill both blanks to get items ordered by 'price' ascending and slice to get items 10 to 20.

Django
items = Product.objects.all().order_by([1])[2]
Drag options to blanks, or click blank then click option'
A'price'
B[:10]
C[10:20]
D'-price'
Attempts:
3 left
💡 Hint
Common Mistakes
Using descending order '-price' instead of ascending.
Using slice [:10] which gets the first 10 items instead of items 10 to 20.
5fill in blank
hard

Fill all three blanks to order by 'category' ascending, then by 'name' descending, and slice the first 15 items.

Django
products = Product.objects.all().order_by([1], [2])[3]
Drag options to blanks, or click blank then click option'
A'category'
B'-name'
C[:15]
D'name'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'name' instead of '-name' for descending order.
Not slicing or slicing incorrectly to get the first 15 items.