Django Python order_by Example Tutorial

Published On: 06/07/2022 | Category: Django Python

Hi Dev,

In this post, we will learn simple steps to sort django queryset in ascending and descending order by date and id. you can see django order by multiple fields. we will help you to give example of django python order_by query set. step by step explain ascending and descending.

Here, django has order_by method to sort the queryset in ascending and descending order. You can order the queryset on any field

Here i explained simply step by step example of how to ascending and descending order django python order_by query set.

Step 1 : Create a Project

In this step, we’ll create a new django project using the django-admin. Head back to your command-line interface and run the following command:

django-admin startproject example
Step 2 : Create a App
python3 manage.py startapp company
Step 3 : Update setting.py

Next, you need to add it in the settings.py file as follows:

....
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'company',
]
Step 4 : Database Setup

Next step, we will modify the settings.py file and update the database settings to configure the mydb database:

settings.py
DATABASES = {  
    'default': {  
        'ENGINE': 'django.db.backends.mysql',  
        'NAME': 'example',  
        'USER':'root',  
        'PASSWORD':'root',  
        'HOST':'localhost',  
        'PORT':'3306'  
    }  
}  
Step 4: Create a Model

In this step we will require the database model for storing contacts.Open the company/models.py file and add the following code:

company/models.py
from django.db import models

# Create your models here.

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

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

In this step, Let’s sort the queryset on ‘id’ in ascending order:

Contact.objects.all().order_by('id')
Sort by Descending Order

In this step, Let’s sort the queryset on ‘id’ in descending order use the not sign ‘-‘ to sort the queryset in reverse order descending order (desc):

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

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

In this step, you can also use the multiple fields to sort the queryset by passing multiple fields as parameters to order_by methods:

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

So, How actually work 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.

I Hope It will help you....