🔥Limited Offer: Get 50% OFFon AI & Full Stack Courses🔥
Back to Django Notes
Topic #38

QuerySet Order By


Order By

To sort QuerySets, Django uses the order_by() method:

Example

mydata = Member.objects.all().order_by('firstname').values()

In SQL, the above statement would be written like this:

SELECT * FROM members ORDER BY firstname;

Descending Order

By default, the result is sorted ascending (the lowest value first), to change the direction to descending (the highest value first), use the minus sign (NOT), - in front of the field name:

Example

mydata = Member.objects.all().order_by('-firstname').values()

In SQL, the above statement would be written like this:

SELECT * FROM members ORDER BY firstname DESC;

Multiple Order Bys

To order by more than one field, separate the fieldnames with a comma in the order_by() method:

Example

mydata = Member.objects.all().order_by('lastname', '-id').values()

In SQL, the above statement would be written like this:

SELECT * FROM members ORDER BY lastname ASC, id DESC;

Want to go beyond the notes?

Join CodingNow 2.0's Django course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

QuerySet Order By – FAQs

Quick answers about learning QuerySet Order By in Django.

This free note from CodingNow 2.0 explains QuerySet Order By in Django — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Django topic on CodingNow 2.0, including QuerySet Order By, is 100% free with no signup required.
With focused practice, most students grasp QuerySet Order By in 1–3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) — expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now