Django Models - How to relate a single class to several variants

120 Views Asked by At

I am defining the models for my app. In this particular case let's say there is a Lecture entity , which I want to render to my template:

class Lecture(models.Model):
title = models.CharField(max_length=128)
section = models.ForeignKey(Section, related_name='section',verbose_name="Lecture section", default=1, null=False, blank=True)
description = models.CharField(max_length = 240, null=True, blank=True)

Also there are different "variants" or "types" of Lectures - lets say PracticalLecture, TheoryLecture. My question is what is best way to structure this:

  • Having a separate Model/Class such as PracticalLecture, TheoryLecture and then linking these back to the Lecture entity via a ForeignKey

or

  • Use a LectureType class that works as as ForeignKey to a Lecture_type field in the Lecture class ?

If you would recommend the latter could you please explain why?

Many thanks!!

1

There are 1 best solutions below

5
On BEST ANSWER

I think you should use the latter approach. Because you would only have two tables, Lecture and LectureType instead of Lecture and many variants.

If you go with the first approach, then all of your variants will have the same columns as the parent Model. If later on, you decided to add another extra column to Lecture, then you have to update all of the variants as well.

I would suggest you to read upon database normalization, here is a very good explaination of this topic and it is kind of similiar to your example