If a workshop is being conducted, there would be some speaker or instructor for a particular workshop. There may be one or more than one instructors for each workshop. So in this cases when we are not sure about how many fields are needed, it is always best to go for another model and add a foreign key to this model. So we are going to do the same here.
In the event/models.py
file, we will create another model to assign instructors for workshops:
class WorkshopInstructor(models.Model):
workshop = models.ForeignKey(Workshop, on_delete=models.CASCADE)
instructor_name = models.CharField(max_length=255)
company_name = models.CharField(max_length=255)
designation = models.CharField(max_length=255)
linked_in_url = models.URLField(blank=True, null=True)
image = models.ImageField(upload_to="instructor_images/", null=True, blank=True)
def __str__(self):
return f"{self.instructor_name}-{self.company_name}"
This model we will be using to note down the details of the instructor of the workshop.
Note : Another type of schema also can be used in this case. The instructor's table can be kept as a separate entitiy where all the details of all instructors will be recorded and in the workshop model a ManyToMany
field can be set to note the instructors of that particular workshop. This avoids duplicate entries in the database. You can try implementing it that way in your project.
So continuing only the model has been created, in order to make changes in the database, migrations need to be run.
python manage.py makemigrations
python manage.py migrate
After this the models have been created in the database.
In the next section, you would see how to register this model in the admin page.