Skip to content

Slot Model

Now, we are going to write a model for Slot. This Slot model will store informations like the campaign with which it is associated, slot date, start time and end time, maximum capacity and reserved capacity of vaccination that can be done.

Look at the class diagram of our project and you can find the Slot class.

Class Diagram

If you study the slot class carefully, then you can find that there is many-to-one relationship between slot and campaign class. That means one slot can be associated with only one campaign and for one campaign there can be multiple slots for vaccination.

Now let us implement this slot class in django.


Open models.py file of Campaign.

First of all, create slot model.

class Slot(models.Model):

Now, add a campaign field to map a many-to-one relationship between slot and campaign.

campaign = models.ForeignKey(Campaign, on_delete=models.CASCADE, null=True)

After that, add the date, start_time and end_time field for the slot.

date = models.DateField(null=True, blank=True)
start_time = models.TimeField(null=True, blank=True)
end_time = models.TimeField(null=True, blank=True)

Each slot has a maximum capacity and reserved quantity for vaccination. We need to add these two field in the slot.

max_capacity = models.IntegerField(default=0, null=True, blank=True)
reserved = models.IntegerField(default=0, null=True, blank=True)

Finally add the str method to represent each slot model in a human readable string.

def __str__(self):
    return str(self.date) + "|" + str(self.start_time) + " to " + str(self.end_time)

Now, register the slot model in django admin panel.

from campaign.models import Campaign, Slot

admin.site.register(Slot)

Create the migration file.

python manage.py makemigrations

Migrate the changes to the database

python manage.py migrate

Start the development server and perform CRUD operations on Slot model.