Let's create a event model which will store the basic details of the event that is going to be conducted. Also let's start that by creating a separate app named event.
The command for creating an app is as follows :
python manage.py startapp event
And now as we have created another app we need to include this in the INSTALLED_APPS
in settings.py
file.
'event',
After this is done, you can open the event/models.py
file and create an abstract model to store event information.
from django.db import models
from authentication.models import Department
from django.utils.text import slugify
from django.core.validators import MinValueValidator, MaxValueValidator
class AbstractEvent(models.Model):
name = models.CharField(max_length=500, unique=True)
year = models.PositiveIntegerField(
validators=[
MinValueValidator(2023),
MaxValueValidator(9999)
]
)
website_link = models.CharField(max_length=1000, blank=True, null=True)
target_audience = models.CharField(max_length=1000,null=True, blank=True)
event_type = models.CharField(max_length=100, choices=[("offline", "Offline"),("online", "Online"), ("both", "Both")], default="offline")
mail_id = models.EmailField()
start_date = models.DateTimeField()
end_date = models.DateTimeField()
registration_start_date = models.DateTimeField()
registration_end_date = models.DateTimeField()
department = models.ForeignKey(Department, on_delete=models.CASCADE)
slug = models.SlugField(max_length=150, unique=True, blank=True)
class Meta:
abstract = True
def save(self, *args, **kwargs):
if not self.slug:
self.slug = slugify(f"{self.name}")
super().save(*args, **kwargs)
def __str__(self):
return self.name
Here this is used as a abstract model which stores some essential details such as,
- Name of the event.
- Year in which the event is taking place.
- Static Website Link if any for the event website.
- Target Audience for the event.
- The mode in which the event is going to take place, wether its an online event or an offline event.
- Official Mail Id for contact, regarding queries for the event.
- Then the start date and end date of events.
- Also the registration start date and registration end date for event is noted.
- A department Foreign Key of the event to note which department is conducting that particular event.
- And at last the slug field, which would be used in the urls. It allows the urls to be in human readable form instead of showing ids in the url.
In the Meta Class, this model is specified as abstract. This means no table would be created for this model in the database, and other classes can inherit the properities of this model.
The save() method is overrided in order to correctly modify the slug field.
So in the next section we would inherit this model to create registrations for workshops.