课程模型分析
分3个表
先设计课程表, 这是1
在设计lesson表,添加一个外键,course.
课程 1 course
章节 n lesson
视频 n video
资源 n courseresourse
课程表的字段如下:
代码
from datetime import datetime
from django.db import models
# Create your models here.
class Courses(models.Model):
name = models.CharField(max_length=50, verbose_name="课程名")
desc = models.CharField(max_length=300, verbose_name="课程描述")
detail = models.TextField(verbose_name="课程详情")
degree = models.CharField(choices=(("cj", "初级"), (\'ZJ\', "中级"), ("GJ", "高级")), verbose_name="课程等级")
learn_times = models.IntegerField(default=0, verbose_name="学习时长(分钟)")
students = models.IntegerField(default=0, verbose_name="学习人数")
fav_nums = models.IntegerField(default=0, verbose_name="收藏人数")
image = models.ImageField(max_length=100, upload_to="courses/%Y/%m/", verbose_name="封面图")
click_nums = models.IntegerField(default=0, verbose_name="点击数")
add_time = models.DateTimeField(default=datetime.now, verbose_name="添加时间")
class Meta:
verbose_name = "课程"
verbose_name_plural = verbose_name
class Lesson(models.Model):
course = models.ForeignKey(Courses, verbose_name="课程") # 看在哪个表添加外键,就看我需要你,就在我里面添加ForeignKey
name = models.CharField(max_length=100, verbose_name="章节名")
add_time = models.DateTimeField(default=datetime.now, verbose_name="添加时间")
class Meta:
verbose_name = "章节"
verbose_name_plural = verbose_name
class Video(models.Model):
lesson = models.ForeignKey(Lesson, verbose_name="章节")
name = models.CharField(max_length=100, verbose_name="章节名称")
add_time = models.DateTimeField(default=datetime.now, verbose_name="添加时间")
class Meta:
verbose_name = "视频"
verbose_name_plural = verbose_name
class CourseResourse(models.Model):
course = models.ForeignKey(Courses, verbose_name="课程名")
name = models.CharField(max_length=100, verbose_name="资源名称")
download = models.FileField(upload_to="course/resource/%Y/%m", verbose_name="资源文件下载地址", max_length=100)
add_time = models.DateTimeField(default=datetime.now, verbose_name="添加时间")
class Meta:
verbose_name = "课程资源"
verbose_name_plural = verbose_name