Perform Query Set – Multiple Order By

Multiple order means first by one and then by other, for multiple Order By you just need to add a second argument in the order_by() method. Now we need to order the data on the basis of Salary and then by Employee Name.

users = EmployeeDetails.objects.all().order_by('Salary','EmployeeName').values()

You can observe in the image below that all these three Employees Amit Patil, Annie Frank, and Tony Stark

has the same Salary but we get the EmployeeName in alphabetical order.

Multiple OrderBy

That’s it for this article. Happy Coding!



Django Query Set – Order By

QuerySet represents a cluster of data in the database. It helps us to slice out the data that we actually need. It is an efficient way to access the data, as it does not perform any database activity until we do something to evaluate the QuerySet. In this tutorial we use order_by() in Django.

Similar Reads

What is Order By?

Order By is a keyword to sort the data either in ascending or descending order. Here in this tutorial, we are using Django Query Set to perform the Order By operation. Let’s create a simple project in Django to store the Employee details....

The basic setup of the Django project

Step 1: Create a Virtual Environment....

Perform Query Set – Multiple Order By

...