Bird
0
0

Which of the following is the correct syntax to order a queryset by the 'name' field in ascending order?

easy📝 Syntax Q3 of 15
Django - DRF Advanced Features
Which of the following is the correct syntax to order a queryset by the 'name' field in ascending order?
AModel.objects.order_by('-name')
BModel.objects.sort('name')
CModel.objects.filter(order='name')
DModel.objects.order_by('name')
Step-by-Step Solution
Solution:
  1. Step 1: Understand order_by syntax

    Ordering ascending uses the field name directly in order_by().
  2. Step 2: Identify correct syntax

    Using '-name' orders descending, filter() and sort() are incorrect methods here.
  3. Final Answer:

    Model.objects.order_by('name') -> Option D
  4. Quick Check:

    Ascending order_by = B [OK]
Quick Trick: Use order_by('field') for ascending order [OK]
Common Mistakes:
MISTAKES
  • Using '-' prefix for ascending order
  • Using filter() instead of order_by()
  • Trying to use sort() which is invalid in Django ORM

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Django Quizzes