Django

College Event Registration Website

CampusConnect Introduction and Setup Configuring settings file for template, static and media files Setting up Tailwind Creating Custom User Model Creating Super User for CampusConnect Registering Custom User Model Search and Filter for User Model Serving Media Files During Development Categorizing Departments Linking Department with HOD Creating Abstract Model for Event Creating Model for Workshop Customizing Admin Page for Workshop Update in Model AbstractEvent Adding Instructor for Workshop Instructor Model Admin Page Adding Poster Field in Abstract Event Providing Access to HOD Access Update for HOD Restricting HOD Access to Particular Department AbstractEvent On Spot Registration Field Creating Workshop Object Creating and Linking Home Page Displaying Workshop on Home Page Styling Home Page Adding Workshop Detail Page Link Workshop Detail Page Workshop Detail Page Styling Workshop Instructor Details Workshop Detail Contact Contact Admin Page Many to Many Field for Contact Displaying Contact on Workshop Detail Page Adding Title for Workshop Detail Page Adding Gallery for Workshop Workshop Gallery Admin Page Displaying Gallery Images on Website Through Context Displaying Gallery Images on Website through template tags Authentication for users User Registration User Registration Submission Logout Functionality For User Login Functionality for User Model For Workshop Registration Workshop Registration Admin Page Register Workshop Function Register Button in Workshop Page Validations Before Workshop Registration Workshop Registration Closed Validaiton User Already Registered for Workshop Validation Workshop Registration Report From Admin Page Export using Library in Django Admin Extending Abstract Event for Hackathons

Order By in Models

In Django, the orderby method is not a built-in method for models. However, you can achieve ordering of querysets by using the order_by() method. The order_by() method allows you to specify how you want the query results to be sorted.

Say you want to sort the Persons with their first name and retrieve the result, you can do that in the following way :

Person.objects.all().order_by('first_name')

alt textAs you can see the result is sorted in ascending order based on the first_name field.

Similarly to sort in descending order, you need to add a (-) sign before the field name as follows :

Person.objects.all().order_by('-first_name')

alt textBecause of the - sign it will return in descending order.

You can order by one or more fields, and you can mix ascending and descending order as needed. Multiple fields are separated by commas:

Person.objects.all().order_by('first_name','-last_name')

alt textIn this example, the records are first sorted in ascending order by the first_name field, and if the first_name field are same, they are then sorted in descending order by the last_name field.

You can also use the order_by() method in combination with other query methods and filters to create complex queries with specific sorting requirements.

Remember that the order_by() method returns a new queryset with the specified ordering, it doesn't modify the original queryset in place. If you want to chain multiple ordering conditions, you can call order_by() multiple times on the same queryset.