Django Sorting

Learn via video courses
Topics Covered

Overview

The Django web framework includes a default object-relational mapping layer (ORM) that can be used to interact with data from various relational databases such as SQLite, PostgreSQL, and MySQL. An object-relational mapper bridges the gap between relational databases and object-oriented programming languages without the need for SQL queries. It standardises interfaces, which reduces boilerplate and accelerates development time. Django allows us to sort the records in ascending order or descending order based on a single field or multiple fields in the table or model, along with addition, deletion, or modifications of the records.

Introduction

Sorting is the operation of arranging the records of a table or list in a specific order based on some ordering criterion. Sorting is performed according to some key value of each record. The records are either numerically or alphabetically sorted. It greatly improves search efficiency. It can be done based on the following factors:

1. Arranging items in a criterion-driven sequence. 2. Grouping items with similar characteristics.

The sorting algorithms must meet the two requirements.

  • The results are in monotonic order.

According to the required order, each element seems to be no smaller/larger than the previous element.* The result represents a permutation of the input.

A reorganization that keeps all of the original elements.

Sorted data is easier to find. Sorting plays a major role in our daily life. Unknowingly, we can use a lot of things efficiently because of the sorting algorithm. Dictionary has all its words sorted alphabetically, which makes it easier for the user to find the meaning of the desired words. Similarly, the names in the contacts list of our phones are also ordered alphabetically for the ease of the user. Applications of sorting include organizing items based on their price on a retail website, sorting the marks obtained by a group of students in a class to rank them, or arranging the names of the students in alphabetical order to assign them their unique identity number.

Order By in Django

Django sorting allows the user to sort the records either alphabetically or in ascending order or descending order based on a single field or multiple fields. Before heading towards Django sorting let us first create a project named Sorting and an app named orderby with model Details.

After the creation of the app we register it in the settings.py file under INSTALLED_APPS,

Now our next task is to create our model in the models.py file,

In the above code,

  • We created a class Details under 'models.model'.
  • We created fields for our model or table that is name, mobile_no, and age.
  • Name is assigned a character field.
  • mobile_no is assigned an integer field.
  • age is assigned an integer field.
  • Underdef __str__, we specified self.name, which implies that our records will display the name when called.

Next, we register it in theadmin.py file,

Now we go back to the command prompt and write the following code to migrate the changes.

Since we are done with the creation of the Django model, we can begin with Django sorting after entering the data or record in the model inside the interactive console.

Let us go to the interactive console by entering the following command.

Inside the interactive console, we import the model Details and create the records and check whether the records we created exist or not. We write the command Details.objects.all() to display the records entered.

To sort the data alphabetically, we write 'name' inside order_by:

To sort the data in the reverse alphabetical order, we write '-name' inside order_by. The minus '-' indicates that the ordering is done in reverse order.

Using Django to Sort Data in Ascending Order (Code Example)

Let us sort the records in ascending order based on a single field. The results are by default sorted in ascending order. Here we are sorting the records based on mobile numbers. Therefore, we write 'mobile_no' inside order_by.

Let us now sort the records based on age. Therefore, we write 'age' insideorder_by.

Using Django to Sort Data in Descending Order (Code Example)

Let us sort the records in descending order based on a single field. Here we are sorting the records based on mobile numbers. Therefore, we write '-mobile_no' inside order_by. The minus '-' indicates that the ordering is done in reverse order.

Let us now sort the records based on age. Therefore, we write '-age' inside order_by. The minus '-' indicates that the ordering is done in reverse order.

Multiple Order By’s (Code Example)

Let us sort the records based on two fields. Here we are sorting the records based on age being in ascending order and mobile number being in descending order. Therefore, in the order_bywe add both the fields separated by a comma.

Conclusion

Hello Developer! I am sure by now you must have got an idea about Django sorting. Let us summarize what we have learned so far

  • Django allows us to sort the records in ascending order or descending order based on a single field or multiple fields in the table or model, along with addition, deletion, or modifications of the records.
  • Django Sorting is the operation of arranging the records of a table or list in a specific order based on some ordering criterion.
  • Sorting is performed according to some key value of each record.
  • Sorted data is easier to find.
  • Django sorting allows the user to sort the records either alphabetically or in ascending order or descending order based on a single field or multiple fields.