• Home
  • Subscribe
  • Contribute Us
    • Share Your Interview Experience
  • Contact Us
  • About
    • About CSEstack
    • Campus Ambassador
  • Forum & Discus
  • Tools for Geek
  • LeaderBoard
CSEstack

What do you want to Learn Today?

  • Programming
    • Tutorial- C/C++
    • Tutorial- Django
    • Tutorial- Git
    • Tutorial- HTML & CSS
    • Tutorial- Java
    • Tutorial- MySQL
    • Tutorial- Python
    • Competitive Coding Challenges
  • CSE Subject
    • (CD) Compiler Design
    • (CN) Computer Network
    • (COA) Computer Organization & Architecture
    • (DBMS) Database Management System
    • (DS) Data Structure
    • (OS) Operating System
    • (ToA) Theory of Automata
    • (WT) Web Technology
  • Interview Questions
    • Interview Questions- Company Wise
    • Interview Questions- Coding Round
    • Interview Questions- Python
    • Interview Questions- REST API
    • Interview Questions- Web Scraping
    • Interview Questions- HR Round
    • Aptitude Preparation Guide
  • GATE 2022
  • Linux
  • Trend
    • Full Stack Development
    • Artificial Intelligence (AI)
    • BigData
    • Cloud Computing
    • Machine Learning (ML)
  • Write for Us
    • Submit Article
    • Submit Source Code or Program
    • Share Your Interview Experience
  • Tools
    • IDE
    • CV Builder
    • Other Tools …
  • Jobs

How to Sort Django QuerySet in Ascending and Descending Order?

Aniruddha Chaudhari/7059/0
Django

In previous tutorial, we have learnt how to create model and form in Django. This tutorial is about reading model data and sorting it.

Let’s say you have this contact model in your Django App.

class ContactModel(models.Model):
   name = models.CharField(max_length=120)
   mobile = models.IntegerField()
   email = models.EmailField()

When we read the data from the model, Django returns queryset. Django has order_by method to sort the queryset in ascending and descending order. You can order the queryset on any field.

In the Django model, there is one autogenerated ‘id’ field. You can use any of the fields (id name, mobile or name) to sort the queryset.

Sort by Ascending Order

Let’s sort the queryset on ‘id’ in ascending order.

ContactModel.objects.all().order_by('id')

Sort by Descending Order (desc)

Use the not sign ‘-‘ to sort the queryset in reverse order aka descending order (desc).

ContactModel.objects.all().order_by('-id')

Note: Here we are reading all the entries from the model. You can also use filter() method instead of all() method to filter out some entries.

Django Order by Multiple Fields

You can also use the multiple fields to sort the queryset by passing multiple fields as parameters to order_by methods.

ContactModel.objects.all().order_by('name', 'mobile')

How does it work?

First, it sorts the queryset by name (first field). If the person has the same name, it sorts queryset by mobile number (second field). If all the entries in the queryset have a unique name, the second field ‘mobile’ does not make any difference.

This order_by method works with any type of model fields like text string, date, time, number, etc.

Usually, models are read and ordered in the Django view function before passing it to the HTML template.

Hope you find this quick tutorial on how sort Django queryset in ascending and descending order helpful. Any doubt? Let me know in the comment.

Related tutorial:

  • How to add model to Django admin site?

Python Interview Questions eBook

Django
Aniruddha Chaudhari
I am complete Python Nut, love Linux and vim as an editor. I hold a Master of Computer Science from NIT Trichy. I dabble in C/C++, Java too. I keep sharing my coding knowledge and my own experience on CSEstack.org portal.

Your name can also be listed here. Got a tip? Submit it here to become an CSEstack author.

Leave a Reply Cancel reply

Prerequisite for Django

Learn Python Basics

Django Tutorials

Why Django?

Django- Create Your First Project

Django- Adding CSS/Static Files

Django- Use/Load Static Files

Django- Create Form

Django- Create Input-Tags

Django- Display Message on Form Submit

Django- MultipleChoiceField in Form

Django- Read Model Data (order_by)

Django- Read First/Last Queryset Object

Django- Update Model Field

Django- Signals

Django- Create TextField in Form

Django- Add Model to Admin Site

Django- Customize Admin Site

Django- Understanding Model User

Django- Registration/Signup Form

Django- Form Layout with Widget-Tweaks

Django- OneToOneField in Model

Django- Foreign Key & Reverse Lookup

Django- Custom Template Filters

Django- Interactive Shell

Django- Social Login

Django- Deploy and Host [FREE] 🔥

© 2022 – CSEstack.org. All Rights Reserved.

  • Home
  • Subscribe
  • Contribute Us
    • Share Your Interview Experience
  • Contact Us
  • About
    • About CSEstack
    • Campus Ambassador
  • Forum & Discus
  • Tools for Geek
  • LeaderBoard