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.
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.