QuerySets को Order करना
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 हो सकता है।
उदाहरण: Why Order Matters
The first call may return chapters in any sequence; the second always returns them sorted by their order field, ascending.
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.
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 उपयोग करता है।
उदाहरण: Ordering by Multiple Fields
Tutorials are grouped by category alphabetically, and within each category they're sorted by their order field.
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. #}
- यह भूल जाना कि querysets का कोई guaranteed order नहीं है जब तक order_by() call न हो, फिर results के randomly shuffle होने पर surprised होना।
- descending order के लिए leading minus sign भूल जाने से ascending और descending के बीच confuse होना।
- एक ऐसी 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 कर सकते हैं।
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: