简体   繁体   English

如何在 Django 中连接多个模型

[英]How can I connect multiple models in Django

I`ma 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.我是 Django 的初学者,想在 Django 模型中创建一个系统,用户可以上传带有标题和不同章节的课程,这些章节都可以包含多个视频。 As seen in this image .如这张图片所示

I thought about creating three models我想过创建三个模型

1. Course 1. 课程

with the course title带有课程名称

2. Chapter 2. 章节

which has different videos in them其中有不同的视频

3. Video 3. 视频

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?我应该如何调整模型并创建连接以完成我所追求的系统?

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).通常, ForeignKey会创建多对一的关系(一个用户可以拥有多门课程,但一门课程不能被多个用户拥有)。 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).并且ManyToManyField创建了ManyToManyField关系(一个学生可以注册多门课程,一个课程也可以有多个学生注册)。 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 ).请注意,related_name 属性是您从类指向的相关对象访问实例(包含关系字段的类的)实例时如何引用它们(示例: https : //docs.djangoproject.com/en/3.1/主题/数据库/查询/#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)

Here is a draft about what you can do:这是一份关于你可以做什么的草稿:
Note that the class names are in CamelCase**请注意,类名在 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM