Calling a class inside another class function

I am trying to create an educational website using django so I have a class model and a course model. I have tried to use the Many-to-one foreignkey relationship but that doesn't work, I can create classes using foreignkey but that class is not being assigned to that course only. It appears in other courses as well. So how can I make this work? What should I change?

My models.py:

class Class(models.Model):
    title = models.CharField(max_length=100)
    video = models.FileField(upload_to='class/class_videos',null=True,
validators=[FileExtensionValidator(allowed_extensions=['MOV','avi','mp4','webm','mkv'])])


    def __str__(self):
        return self.title


class Course(models.Model):
    title = models.CharField(max_length=100)
    image = models.ImageField(upload_to='class/instructor_pics', null=True)
    instructor = models.CharField(max_length=100)
    instructor_image = models.ImageField(upload_to='class/instructor_pics', null=True)
    students = models.ManyToManyField(User, related_name='courses_joined', blank=True)
    classes = models.ForeignKey(Class, on_delete=models.CASCADE, null=True)
    slug = models.SlugField(max_length=200, unique=True)
    description = models.TextField(max_length=300, null=True)
    created = models.DateTimeField(auto_now_add=True)

    class Meta:
        ordering = ['-created']

    def __str__(self):
        return self.title

You are using the foreign key in the wrong model. If each class can only have one course, but a single course, can have multiple classes, you should place the ForeignKey in the class model instead of the course model. Your code would be like this:

class Course(models.Model):
    title = models.CharField(max_length=100)
    image = models.ImageField(upload_to='class/instructor_pics', null=True)
    instructor = models.CharField(max_length=100)
    instructor_image = models.ImageField(upload_to='class/instructor_pics', null=True)
    students = models.ManyToManyField(User, related_name='courses_joined', blank=True)

    slug = models.SlugField(max_length=200, unique=True)
    description = models.TextField(max_length=300, null=True)
    created = models.DateTimeField(auto_now_add=True)

    class Meta:
        ordering = ['-created']

    def __str__(self):
        return self.title
class Class(models.Model):
    title = models.CharField(max_length=100)
    video = models.FileField(upload_to='class/class_videos',null=True,
validators=[FileExtensionValidator(allowed_extensions=['MOV','avi','mp4','webm','mkv'])])
    course = models.ForeignKey(Course, on_delete=models.CASCADE, null=True, related_name='classes')


def __str__(self):
    return self.title

And when you want to list the classes of a single course, you can use this code (you should use the related_name field in the source model like the way I have used in the class model):

course = Course.objects.filter(some_filter=some_value).first()
course.classes.first() # This will return the first class of the course
Back to Top