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

Static Method in Models

In Django models, static methods can also be added. These methods can directly be called using the Model's name. There is no need to create an object in order to access the method.

For example, lets add the static method inside the Person model.

class Person(models.Model):
    first_name = models.CharField(max_length=30)
    last_name = models.CharField(max_length=30)
    full_name = models.CharField(max_length=60, null=True)
    birth_date = models.DateField()
    is_active = models.BooleanField(default=True)
    email = models.EmailField(unique=True, null=True)
    username = models.CharField(max_length=30,unique=True, null=True)


    def __str__(self):
        return f"{self.first_name} {self.last_name}"
    
    class Meta:
        unique_together = ('first_name', '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)

    def calculate_age(self):
        today = date.today()
        age = today.year - self.birth_date.year
        return age

    @staticmethod
    def get_persons_by_dob():
        return Person.objects.all().order_by('birth_date')

Now you can call the get_persons_by_dob() method, without creating the object of the Person class.

from modeldemo.models import Person
Person.get_persons_by_dob()

alt text Similarly static methods can also be created with parameters.

@staticmethod
def get_persons(startswith=''):
    return Person.objects.filter(first_name__startswith=startswith)

If the startswith parameter is given, then it will return the values with wich the name starts, else it will return all the person objects.

Without Parameter :

from modeldemo.models import Person
Person.get_persons()

alt text With Parameter :

Person.get_persons('J')

alt text