JonasLevin
JonasLevin

Reputation: 2089

How can I connect multiple models in Django

I`m a beginner in Django and want to create a system in the Django-models were a user can upload a course with a title and different Chapters which all can have multiple videos in them. As seen in this image.

I thought about creating three models

1. Course

with the course title

2. Chapter

which has different videos in them

3. Video

And here the video with a title of the video

But I have no Idea how to create a connection between those three models, so that there can be multiple chapters in one course and multiple videos in one chapter.

These are my first models (I just created them quickly):

def user_directory_path(instance, filename):
    # file will be uploaded to MEDIA_ROOT/user_<id>/<filename>
    return 'user_{0}/{1}'.format(instance.user.id, filename)


class video(models.Model):
    title = models.CharField(max_length=100)
    video = models.FileField(upload_to=user_directory_path)

class course(models.Model):
    title = models.CharField(max_length=100)

class chapter(models.Model):
    pass

How should I adjust the models and create the connection to accomplish the system that I´m after?

Upvotes: 1

Views: 2332

Answers (2)

progdev
progdev

Reputation: 71

You can use the below models as a foundation. Generally, ForeignKey creates a many-to-one relationship (one user can own multiple courses, but one course cannot be owned by multiple users). And ManyToManyField creates a many-to-many relationship (one student can be enrolled in multiple courses, and also one course can have multiple students enrolled). Note that the related_name attribute is how you refer back to the instances (of the class containing the relational field) when accessing them from the related object the class points to (examples: https://docs.djangoproject.com/en/3.1/topics/db/queries/#backwards-related-objects).

from django.contrib.auth.models import User
from django.db import models

    
class Subject(models.Model):
    title = models.CharField(max_length=200)
    
class Course(models.Model):
    owner = models.ForeignKey(User, related_name='courses_created', on_delete=models.CASCADE)
    subject = models.ForeignKey(Subject, related_name='courses', on_delete=models.CASCADE)
    title = models.CharField(max_length=200)
    created = models.DateTimeField(auto_now_add=True)
    students = models.ManyToManyField(User, related_name='courses_joined', blank=True)

class Chapter(models.Model):
    course = models.ForeignKey(Course, related_name='chapters', on_delete=models.CASCADE)
    title = models.CharField(max_length=200)
    description = models.TextField(blank=True)
    
class VideoItem(models.Model):
    title = models.CharField(max_length=250)
    video = models.FileField(upload_to=user_directory_path)
    chapter = models.ForeignKey(Chapter, related_name='videos', on_delete=models.CASCADE)
    created = models.DateTimeField(auto_now_add=True)

Upvotes: 2

Nadhem Maaloul
Nadhem Maaloul

Reputation: 433

Here is a draft about what you can do:
Note that the class names are in CamelCase**

class Course(models.Model):
    title = models.CharField(max_length=100)

class Chapter(models.Model):
    course = models.ForeignKey(Course, on_delete=models.CASCADE)

class Video(models.Model):
    title = models.CharField(max_length=100)
    video = models.FileField(upload_to=user_directory_path)
    chapter = models.ForeignKey('Chapter', on_delete=models.CASCADE)

Upvotes: 0

Related Questions