2 回答
TA贡献1804条经验 获得超8个赞
您可以使用以下模型作为基础。通常,ForeignKey
创建多对一关系(一个用户可以拥有多个课程,但一门课程不能由多个用户拥有)。并ManyToManyField
创建多对多的关系(一个学生可以注册多门课程,一门课程可以有多个学生注册)。请注意, related_name 属性是您从类指向的相关对象访问实例(包含关系字段的类的实例)时引用它们的方式。
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)
TA贡献1796条经验 获得超7个赞
以下是关于您可以执行的操作的草稿:
请注意,类名称采用 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)
添加回答
举报