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

Overriding save() method

In the previous topic, you saw how you can update the records of persons who were born before 1960. Now, if any new record is added or the birth_date of any previous record is updated, you will need to run the update() to set is_active to False everytime. This is not an eficient way to update() the records. There is an alternate way to update the records which is to override the default save() method. By this, any time if a new object is created or an existing object is updated, it will check the birth_date and update the is_active field accordingly. The save() method can be overrided as follows :

from datetime import datetime
class Person(models.Model):
    first_name = models.CharField(max_length=30)
    last_name = models.CharField(max_length=30)
    birth_date = models.DateField()
    is_active = models.BooleanField(default=True)

    def __str__(self):
        return f"{self.first_name} {self.last_name}"
    
    def save(self, *args, **kwargs):
        if isinstance(self.birth_date, str):
            self.birth_date = datetime.strptime(self.birth_date, '%Y-%m-%d').date()
        if self.birth_date.year<1960:
            self.is_active=False
        super().save(*args, **kwargs)

So here, first if the argument is of string type, it is converted into date and then if the year is less than 1960, the is_active field is updated to False and then the default save() method of the model is called.

So now, if you create a new object or update any existing object, according to the condition the is_active field would be changed automatically. For example, change the birth_date field where id=1 :

p=Person.objects.get(id=1)
p.birth_date = '1959-01-01'
p.save()

alt textAs you can see, the is_active column on calling the save() method is updated automatically when the birth_date value changes.