Skip to content

Storage Model

Now, we are going to write a model for Storage. This Storage model represents a space in the vaccination center where we are going to store vaccines.

Look at the class diagram of our project. There is a storage class that has attributes like center, vaccine, total_quantity, booked_quantity.

Class Diagram

Now, if you see very carefully at the storage class, then you can notice that the storage class is associated with center and vaccine class.

Actually, there is a many-to-one relationship between storage-center and storage-vaccine table.

Let's try to understand what it means.

Suppose you have two vaccines: A and B and there are two centers 1 and 2.

Now, one center can have many storages. Because technically, in one center, we can store multiple vaccines. So, in centers 1 and 2, we have created two storages one for vaccine A and one for vaccine B.

Database Design

Therefore one center can have multiple storage and one storage can belong to only one center. So, there is many-to-one relationship between the storage and center.

Also, if you notice carefully, one vaccine can have multiple storages in different centers and one storage can have only one vaccine. So, there is many-to-one relationship between storage and vaccine.

Now, we have understood the relationship between center - storage and vaccine - storage, let us try to write a model for Storage and understand how to implement many-to-one relationships in Django.

Open your models.py file of Center.

First import the vaccine model.

from vaccine.models import Vaccine

Now, create a model named Storage:

class Storage(models.Model):

To implement many-to-one relationships in Django, we need to use ForeignKey.

center = models.ForeignKey(Center, on_delete=models.CASCADE)

Here, we are using ForeignKey as a model field type and it takes the Model name "Center" with which we want to build one-to-many relationship, as a first argument.

We also need to provide on_delete=models.CASCADE. This means that if any center object gets deleted, then the corresponding storage object will also be deleted automatically from the database.

So, this statement tells that one storage can be associated with only one center and one center can have multiple storages.

Let us add another model field named vaccine.

vaccine = models.ForeignKey(Vaccine, on_delete=models.CASCADE)

Here, we are using ForeignKey as the model field type and it takes the Model name "Vaccine" with which we want to build one-to-many relationship, as a first argument.

We have written on_delete=models.CASCADE. This means that if any vaccine object gets deleted, then the corresponding storage object will also be deleted from the database.

So, this statement tells us that one storage can have only one vaccine and one vaccine can have multiple storages.

Now, add the other fields.

total_quantity = models.IntegerField(default=0)

Here, the total_quantity stores the total number of vaccines present in the storage of the vaccination center.

booked_quantity = models.IntegerField(default=0)

Here, the booked_quantity stores the total number of vaccines that are reserved for new vaccination of patients in that center.

Finally, add the str method.

def __str__(self):
    return self.center.name + " | " + self.vaccine.name

So, using this str method, we will represent every storage object with the center name and vaccine name.


Now, we have written the model for Storage, let us create a new migration file.

python manage.py makemigrations

Then, migrate the changes into the database:

python manage.py migrate

After that, register the Storage model in the admin panel. Open admin.py file of Center.

from center.models import Center, Storage

admin.site.register(Storage)

Finally, Open the admin panel and we can see the Storage model in the admin panel.

Perform CRUD operation with the Storage model.