[py]django课程模型

Posted 毛台

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[py]django课程模型相关的知识,希望对你有一定的参考价值。

课程模型分析

分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

以上是关于[py]django课程模型的主要内容,如果未能解决你的问题,请参考以下文章

[py][mx]django实现根据城市和课程机构类别过滤

Django?模型(数据库)

09 Django 模型(数据库)

Django模型(数据库)008

[py][mx]django模板继承-课程列表页

第三百七十五节,Django+Xadmin打造上线标准的在线教育平台—创建课程机构app,在models.py文件生成3张表,城市表课程机构表讲师表