← Back to Django Course | Chapter 6: Advanced ORM Queries | Lesson 2 of 9

QuerySets को Order करना

एक queryset को order करना toys को scattered छोड़ने के बजाय size से line up करने जैसा है — आप decide करते हैं कि results किस order में वापस आएं।
Syntax
markup
ModelName.objects.order_by('field')     # ascending
ModelName.objects.order_by('-field')    # descending
ModelName.objects.order_by('field1', '-field2')

Order क्यों मायने रखता है

order_by() के बिना, Django किसी particular order का promise नहीं करता — database rows को जिस भी order में इसके लिए fastest हो वापस कर सकता है, जो calls के बीच change हो सकता है।

Note: जब display order users के लिए मायने रखे तो हमेशा order_by() जोड़ें।

उदाहरण: Why Order Matters

The first call may return chapters in any sequence; the second always returns them sorted by their order field, ascending.

markup
from core.models import Chapter

# Order is not guaranteed
Chapter.objects.all()

# Order is now guaranteed by 'order' field
Chapter.objects.order_by('order')
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}

Ascending और Descending Order

title जैसा एक plain field name smallest/earliest को पहले sort करता है (ascending); इसके आगे एक minus sign लगाना, जैसे '-title', इसे descending में reverse कर देता है।

उदाहरण: Ascending and Descending Order

The second query returns the exact same tutorials as the first, just in reverse alphabetical order.

markup
from core.models import Tutorial

# A to Z
Tutorial.objects.order_by('title')

# Z to A
Tutorial.objects.order_by('-title')
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}

Multiple Fields से Ordering

आप order_by() को कई field names pass कर सकते हैं; Django पहले field से sort करता है, और ties तोड़ने के लिए next fields उपयोग करता है।

Note: सबसे important sort key पहले list करें।

उदाहरण: Ordering by Multiple Fields

Tutorials are grouped by category alphabetically, and within each category they're sorted by their order field.

markup
from core.models import Tutorial

# Sort by category, then by order within each category
Tutorial.objects.order_by('category', 'order')
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. यह भूल जाना कि querysets का कोई guaranteed order नहीं है जब तक order_by() call न हो, फिर results के randomly shuffle होने पर surprised होना।
  2. descending order के लिए leading minus sign भूल जाने से ascending और descending के बीच confuse होना।
  3. एक ऐसी field से order करना जो model पर मौजूद नहीं है, जो silently ignore करने के बजाय एक FieldError raise करता है।
चैप्टर सारांश
  • order_by() control करता है कि results किस order में return हों।
  • एक plain field name ascending sort करता है; एक leading - descending sort करता है।
  • आप उन्हें priority order में list करके multiple fields से order कर सकते हैं।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.