Skip to content

Campaign Model

Now, we are going to write a model for Campaign. This Campaign model will store informations like center, vaccine, start_date, end_date and agents.

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

Class Diagram

If you look at the Campaign class carefully, then you can notice that there is a column named agents. Using this agents, there is a many-to-many relationship between agents and user model.

It means that one user can become agent of multiple campaign and in one campaign there can be multiple agents.

Therefore, there is many-to-many relationship between user and campaign model.

Also, there is many-to-one relationship between campaign - center and campaign - vaccine as you can see in the class diagram.


Now, open models.py file of Campaign app.

First we need to import Center, Vaccine and User model because we are going to need it while building the relationship with Campaign model.

from center.models import Center
from vaccine.models import Vaccine
from django.contrib.auth import get_user_model

User = get_user_model()

Django comes with built-in user authentication and authorization system which consists of User model. So, to import that User model, we need to use get_user_model method from contrib.auth library.

Now, create a model for Campaign.

class Campaign(models.Model):

After that, create a center field for implementing many-to-one relationship between campaign and center model.

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

After that, create a vaccine field for implementing many-to-one relationship between campaign and vaccine model.

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

After that, create start_date and end_date field that stores when the campaign will start and when the campaign will end.

start_date = models.DateField(null=True)
end_date = models.DateField(null=True)

After that, create agents field for implementing many-to-many relationship between campaign and user model.

To implement many-to-many relationship we need to use ManyToManyField as model field type. The first argument will be the model name i.e. User with which we want to build many-to-many relationship.

agents = models.ManyToManyField(User, blank=True)

When you use ManyToManyField, then django automatically sets null = True in that field.

We have written blank=True to tell the Django Template engine to ignore the required condition in this agents field. So, if you submit a form keeping this agents field blank then Django will not raise any error.

Finally, add the str method to represent each campaign object with a human readable string.

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

Now, add this Campaign model in django admin panel.

Add this code in admin.py file of Campaign.

from campaign.models import Campaign

admin.site.register(Campaign)

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 Campaign model.