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

String Representation for Model

In the previous topic, you had seen how to create an object for a model, and after creating an object you got something like this :

<Person: Person object (1)>

alt text The name <Person: Person object (1)> is the name give by django as a default value. This is not in human readable format. You can convert this into human readable format by defining a __str__ method in your model. Converting into human readable format would be helpful when working with the django admin interface or when debugging the code.

The __str__ method can be defined as follows for the Person model:

def __str__(self):
        return f"{self.first_name} {self.last_name}"

Here the first name and last name of the person is concatenated and returned by the function.

If you try to create an object for the model now, it would give an output in this format.

Person.objects.create(first_name='Programming', last_name='Master', birth_date='1992-10-15')

alt text Here you will get an output like this :

<Person: Programming Master>

Which is the same as specified in the __str__ function. Note : After editing the models.py file, you will need to restart the shell.

Similarly, if you try to view all the objects, the first object also will come in this format.

Person.objects.all()

alt text