课程所需要的数据库表有:
- 存放课程基本信息的
- 存放课程章节的
- 存放章节视频的
- 存放课程资源的
其中1-2是1对多的关系,2-3也是1对多的关系,1-4也是1对多的关系。django没单独设置一对多或者多对一额映射关系,靠外键ForeignKey实现。models.py:
from django.db import models
from datetime import datetime
# Create your models here.
class Course(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","高级")),max_length=10)
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(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(Course,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 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 CoursesResources(models.Model):
course = models.ForeignKey(Course, verbose_name="课程")
name = models.CharField(max_length=100, verbose_name="名称")
download = models.FileField(upload_to="course/resource/%Y/%m", verbose_name="资源文件")
add_time = models.DateTimeField(default=datetime.now, verbose_name="添加时间")
class Meta:
verbose_name = "课程资源"
verbose_name_plural = verbose_name
可以大致看到外间的用法。
course = models.ForeignKey(Course, verbose_name="课程")
括号里先是对应的表格(类)名,然后再起个名字~