• 课程表设计以及优化


    课程表设计以及优化

    ForeignKey字段的扩展:

    related_name:

    反向操作时,使用的字段名,用来替换原反向查询时的表名_set

    related_query_name:

    反向查询操作时,使用的连接前缀,用于替换表名。

    课程表分析(简陋版)

    from django.db import models
    
    class Course(models.Model):
        name = models.CharField(max_length=64)
        title = models.CharField(max_length=64)
        level = models.IntegerField(choices=((0, '入门'), (1, '进阶')), default=0)
        detail = models.TextField()  # 可以关联详情表
        type = models.IntegerField(choices=((0, 'Python'), (1, 'Linux')), default=0)
        is_show = models.BooleanField(default=False)
    
        # 优化字段
        # 总学生数
        students = models.IntegerField(default=0)
        # 总学时
        time = models.IntegerField(default=0)
    
    
        class Meta:
            abstract = True  # 虚拟机表
    
    
    # 免费课
    class FreeCourse(Course):
        image = models.ImageField(upload_to='course/free')
        attachment = models.FileField(upload_to='attachment')
    
    # 实战课
    class ActualCourse(Course):
        image = models.ImageField(upload_to='course/actual')
        price = models.DecimalField(max_digits=7, decimal_places=2)
        cost = models.DecimalField(max_digits=7, decimal_places=2)
    
    # 轻课
    class LightCourse(Course):
        image = models.ImageField(upload_to='course/light')
        price = models.DecimalField(max_digits=7, decimal_places=2)
        cost = models.DecimalField(max_digits=7, decimal_places=2)
        period = models.IntegerField(verbose_name='学习建议周期(month)', default=0)
    

    课程表进阶

    #  路飞课程相关表,以免费课为例
    from luffyapi.utils.models import BaseModel
    from itertools import chain   
    
    class CourseCategory(BaseModel):
        """
        课程分类
        python,linux,go, 网络安全
        分类与课程是一对多关系
        """
        name = models.CharField(max_length=64, unique=True, verbose_name="分类名称")
    
        class Meta:
            db_table = "luffy_course_category"  # 修改表名
            verbose_name = "分类"
            verbose_name_plural = verbose_name
    
        def __str__(self):
            return "%s" % self.name
    
    
    class Course(BaseModel):
        """课程表"""
        course_type = (
            (0, '付费'),
            (1, 'VIP专享'),
            (2, '学位课程')
        )
        level_choices = (
            (0, '初级'),
            (1, '中级'),
            (2, '高级'),
        )
        status_choices = (
            (0, '上线'),
            (1, '下线'),
            (2, '预上线'),
        )
        # 原始字段
        name = models.CharField(max_length=128, verbose_name="课程名称")
        course_img = models.ImageField(upload_to="courses", max_length=255, verbose_name="封面图片", blank=True, null=True)
        course_type = models.SmallIntegerField(choices=course_type, default=0, verbose_name="付费类型")
        # 使用这个字段的原因
        brief = models.TextField(max_length=2048, verbose_name="详情介绍", null=True, blank=True)
        level = models.SmallIntegerField(choices=level_choices, default=0, verbose_name="难度等级")
        pub_date = models.DateField(verbose_name="发布日期", auto_now_add=True)
        period = models.IntegerField(verbose_name="建议学习周期(day)", default=7)
        attachment_path = models.FileField(upload_to="attachment", max_length=128, verbose_name="课件路径", blank=True,
                                           null=True)
        status = models.SmallIntegerField(choices=status_choices, default=0, verbose_name="课程状态")
        price = models.DecimalField(max_digits=6, decimal_places=2, verbose_name="课程原价", default=0)
    
        # 优化字段
        students = models.IntegerField(verbose_name="学习人数", default=0)
        sections = models.IntegerField(verbose_name="总课时数量", default=0)
        pub_sections = models.IntegerField(verbose_name="课时更新数量", default=0)
    
        # 关联字段
        teacher = models.ForeignKey("Teacher", on_delete=models.DO_NOTHING, null=True, blank=True, verbose_name="授课老师")
        course_category = models.ForeignKey("CourseCategory", on_delete=models.SET_NULL, db_constraint=False, null=True,
                                            blank=True, verbose_name="课程分类")
    
        class Meta:
            db_table = "luffy_course"
            verbose_name = "课程"
            verbose_name_plural = "课程"
    
        def __str__(self):
            return "%s" % self.name
    
        @property
        def course_type_name(self):
            """课程类型"""
            return self.get_course_type_display()
    
        @property
        def level_name(self):
            """课程级别"""
            return self.get_level_display()
    
        @property
        def status_name(self):
            """课程状态"""
            return self.get_status_display()
    
        @property
        def section_list(self):
            """
            section_info: the list of sections
            one course has many chapters
            one chapter has many sections
            """
            section_info = []
            # 根据课程来查询章节(一对多),反向查询
            # 根据章节取出所有的课时,反向查询
            chapter_list = [chapter.coursesections.all() for chapter in self.coursechapters.all()]
            section_lst = list(chain.from_iterable(chapter_list))   # 将二维列表变成一维列表
            attrs = ['name', 'section_link', 'duration', 'free_trail']
            for section in section_lst:
                section_info.append(
                    {attr: getattr(section, attr, None) for attr in attrs}
                )
            return section_info[:4]
    
    
    class Teacher(BaseModel):
        """
        导师表
        跟课程一对多,关联字段写在课程中
        """
        role_choices = (
            (0, '讲师'),
            (1, '导师'),
            (2, '班主任'),
        )
        name = models.CharField(max_length=32, verbose_name="导师名")
        role = models.SmallIntegerField(choices=role_choices, default=0, verbose_name="导师身份")
        title = models.CharField(max_length=64, verbose_name="职位、职称")
        signature = models.CharField(max_length=255, verbose_name="导师签名", help_text="导师签名", blank=True, null=True)
        image = models.ImageField(upload_to="teacher", null=True, verbose_name="导师封面")
        brief = models.TextField(max_length=1024, verbose_name="导师描述")
    
        class Meta:
            db_table = "luffy_teacher"
            verbose_name = "导师"
            verbose_name_plural = verbose_name
    
        def __str__(self):
            return "%s" % self.name
    
        @property
        def role_name(self):
            """老师身份"""
            return self.get_role_display()
    
    
    class CourseChapter(BaseModel):
        """
        章节表
        章节跟课程一对多关系  (课程有多个章节)
        """
        course = models.ForeignKey("Course", related_name='coursechapters', on_delete=models.CASCADE, verbose_name="课程名称")
        chapter = models.SmallIntegerField(verbose_name="第几章", default=1)
        name = models.CharField(max_length=128, verbose_name="章节标题")
        summary = models.TextField(verbose_name="章节介绍", blank=True, null=True)
        pub_date = models.DateField(verbose_name="发布日期", auto_now_add=True)
    
        class Meta:
            db_table = "luffy_course_chapter"
            verbose_name = "章节"
            verbose_name_plural = verbose_name
    
        def __str__(self):
            return "%s:(第%s章)%s" % (self.course, self.chapter, self.name)
    
    
    class CourseSection(BaseModel):
        """
        课时表
        课时和章节是一对多的关系,关联字段在多的一方,课时
        """
        section_type_choices = (
            (0, '文档'),
            (1, '练习'),
            (2, '视频')
        )
        chapter = models.ForeignKey("CourseChapter", related_name='coursesections', on_delete=models.CASCADE,
                                    verbose_name="课程章节")
        name = models.CharField(max_length=128, verbose_name="课时标题")
        orders = models.PositiveSmallIntegerField(verbose_name="课时排序")
        section_type = models.SmallIntegerField(default=2, choices=section_type_choices, verbose_name="课时种类")
        section_link = models.CharField(max_length=255, blank=True, null=True, verbose_name="课时链接",
                                        help_text="若是video,填vid,若是文档,填link")
        duration = models.CharField(verbose_name="视频时长", blank=True, null=True, max_length=32)  # 仅在前端展示使用
        pub_date = models.DateTimeField(verbose_name="发布时间", auto_now_add=True)
        free_trail = models.BooleanField(verbose_name="是否可试看", default=False)
    
        class Meta:
            db_table = "luffy_course_Section"
            verbose_name = "课时"
            verbose_name_plural = verbose_name
    
        def __str__(self):
            return "%s-%s" % (self.chapter, self.name)
    
    

    xadmin界面

    数据的录入

    老师表

    INSERT INTO luffy_teacher(id, orders, is_show, is_delete, created_time, updated_time, name, role, title, signature, image, brief) VALUES (1, 1, 1, 0, '2019-07-14 13:44:19.661327', '2019-07-14 13:46:54.246271', 'Alex', 1, '老男孩Python教学总监', '金角大王', 'teacher/alex_icon.png', '老男孩教育CTO & CO-FOUNDER 国内知名PYTHON语言推广者 51CTO学院20162017年度最受学员喜爱10大讲师之一 多款开源软件作者 曾任职公安部、飞信、中金公司、NOKIA中国研究院、华尔街英语、ADVENT、汽车之家等公司');
    
    INSERT INTO luffy_teacher(id, orders, is_show, is_delete, created_time, updated_time, name, role, title, signature, image, brief) VALUES (2, 2, 1, 0, '2019-07-14 13:45:25.092902', '2019-07-14 13:45:25.092936', 'Mjj', 0, '前美团前端项目组架构师', NULL, 'teacher/mjj_icon.png', '是马JJ老师, 一个集美貌与才华于一身的男人,搞过几年IOS,又转了前端开发几年,曾就职于美团网任高级前端开发,后来因为不同意王兴(美团老板)的战略布局而出家做老师去了,有丰富的教学经验,开起车来也毫不含糊。一直专注在前端的前沿技术领域。同时,爱好抽烟、喝酒、烫头(锡纸烫)。 我的最爱是前端,因为前端妹子多。');
    
    INSERT INTO luffy_teacher(id, orders, is_show, is_delete, created_time, updated_time, name, role, title, signature, image, brief) VALUES (3, 3, 1, 0, '2019-07-14 13:46:21.997846', '2019-07-14 13:46:21.997880', 'Lyy', 0, '老男孩Linux学科带头人', NULL, 'teacher/lyy_icon.png', 'Linux运维技术专家,老男孩Linux金牌讲师,讲课风趣幽默、深入浅出、声音洪亮到爆炸');
    

    分类表

    INSERT INTO luffy_course_category(id, orders, is_show, is_delete, created_time, updated_time, name) VALUES (1, 1, 1, 0, '2019-07-14 13:40:58.690413', '2019-07-14 13:40:58.690477', 'Python');
    
    INSERT INTO luffy_course_category(id, orders, is_show, is_delete, created_time, updated_time, name) VALUES (2, 2, 1, 0, '2019-07-14 13:41:08.249735', '2019-07-14 13:41:08.249817', 'Linux');
    

    课程表

    INSERT INTO luffy_course(id, orders, is_show, is_delete, created_time, updated_time, name, course_img, course_type, brief, level, pub_date, period, attachment_path, status, students, sections, pub_sections, price, course_category_id, teacher_id) VALUES (1, 1, 1, 0, '2019-07-14 13:54:33.095201', '2019-07-14 13:54:33.095238', 'Python开发21天入门', 'courses/alex_python.png', 0, 'Python从入门到入土&&&Python从入门到入土&&&Python从入门到入土&&&Python从入门到入土&&&Python从入门到入土&&&Python从入门到入土&&&Python从入门到入土&&&Python从入门到入土&&&Python从入门到入土&&&Python从入门到入土&&&Python从入门到入土&&&Python从入门到入土', 0, '2019-07-14', 21, '', 0, 231, 120, 120, 0.00, 1, 1);
    
    INSERT INTO luffy_course(id, orders, is_show, is_delete, created_time, updated_time, name, course_img, course_type, brief, level, pub_date, period, attachment_path, status, students, sections, pub_sections, price, course_category_id, teacher_id) VALUES (2, 2, 1, 0, '2019-07-14 13:56:05.051103', '2019-07-14 13:56:05.051142', 'Python项目实战', 'courses/mjj_python.png', 0, '', 1, '2019-07-14', 30, '', 0, 340, 120, 120, 99.00, 1, 2);
    
    INSERT INTO luffy_course(id, orders, is_show, is_delete, created_time, updated_time, name, course_img, course_type, brief, level, pub_date, period, attachment_path, status, students, sections, pub_sections, price, course_category_id, teacher_id) VALUES (3, 3, 1, 0, '2019-07-14 13:57:21.190053', '2019-07-14 13:57:21.190095', 'Linux系统基础5周入门精讲', 'courses/lyy_linux.png', 0, '', 0, '2019-07-14', 25, '', 0, 219, 100, 100, 39.00, 2, 3);
    

    章节表

    INSERT INTO luffy_course_chapter(id, orders, is_show, is_delete, created_time, updated_time, chapter, name, summary, pub_date, course_id) VALUES (1, 1, 1, 0, '2019-07-14 13:58:34.867005', '2019-07-14 14:00:58.276541', 1, '计算机原理', '', '2019-07-14', 1);
    
    INSERT INTO luffy_course_chapter(id, orders, is_show, is_delete, created_time, updated_time, chapter, name, summary, pub_date, course_id) VALUES (2, 2, 1, 0, '2019-07-14 13:58:48.051543', '2019-07-14 14:01:22.024206', 2, '环境搭建', '', '2019-07-14', 1);
    
    INSERT INTO luffy_course_chapter(id, orders, is_show, is_delete, created_time, updated_time, chapter, name, summary, pub_date, course_id) VALUES (3, 3, 1, 0, '2019-07-14 13:59:09.878183', '2019-07-14 14:01:40.048608', 1, '项目创建', '', '2019-07-14', 2);
    
    INSERT INTO luffy_course_chapter(id, orders, is_show, is_delete, created_time, updated_time, chapter, name, summary, pub_date, course_id) VALUES (4, 4, 1, 0, '2019-07-14 13:59:37.448626', '2019-07-14 14:01:58.709652', 1, 'Linux环境创建', '', '2019-07-14', 3);
    

    课时表

    INSERT INTO luffy_course_Section(id, is_show, is_delete, created_time, updated_time, name, orders, section_type, section_link, duration, pub_date, free_trail, chapter_id) VALUES (1, 1, 0, '2019-07-14 14:02:33.779098', '2019-07-14 14:02:33.779135', '计算机原理上', 1, 2, NULL, NULL, '2019-07-14 14:02:33.779193', 1, 1);
    
    INSERT INTO luffy_course_Section(id, is_show, is_delete, created_time, updated_time, name, orders, section_type, section_link, duration, pub_date, free_trail, chapter_id) VALUES (2, 1, 0, '2019-07-14 14:02:56.657134', '2019-07-14 14:02:56.657173', '计算机原理下', 2, 2, NULL, NULL, '2019-07-14 14:02:56.657227', 1, 1);
    
    INSERT INTO luffy_course_Section(id, is_show, is_delete, created_time, updated_time, name, orders, section_type, section_link, duration, pub_date, free_trail, chapter_id) VALUES (3, 1, 0, '2019-07-14 14:03:20.493324', '2019-07-14 14:03:52.329394', '环境搭建上', 1, 2, NULL, NULL, '2019-07-14 14:03:20.493420', 0, 2);
    
    INSERT INTO luffy_course_Section(id, is_show, is_delete, created_time, updated_time, name, orders, section_type, section_link, duration, pub_date, free_trail, chapter_id) VALUES (4, 1, 0, '2019-07-14 14:03:36.472742', '2019-07-14 14:03:36.472779', '环境搭建下', 2, 2, NULL, NULL, '2019-07-14 14:03:36.472831', 0, 2);
    
    INSERT INTO luffy_course_Section(id, is_show, is_delete, created_time, updated_time, name, orders, section_type, section_link, duration, pub_date, free_trail, chapter_id) VALUES (5, 1, 0, '2019-07-14 14:04:19.338153', '2019-07-14 14:04:19.338192', 'web项目的创建', 1, 2, NULL, NULL, '2019-07-14 14:04:19.338252', 1, 3);
    
    INSERT INTO luffy_course_Section(id, is_show, is_delete, created_time, updated_time, name, orders, section_type, section_link, duration, pub_date, free_trail, chapter_id) VALUES (6, 1, 0, '2019-07-14 14:04:52.895855', '2019-07-14 14:04:52.895890', 'Linux的环境搭建', 1, 2, NULL, NULL, '2019-07-14 14:04:52.895942', 1, 4);
    
  • 相关阅读:
    pipenv install
    git删除缓存区中文件
    zsh切换bash bash切换zsh
    Mac下安装allure
    Linux基础命令之tail动态显示日志文件时关键字有颜色、高亮显示
    CentOS添加tailf命令
    阿里云服务器添加安全组,允许url+端口访问
    pytest中用例的识别与运行
    [转] 使用webpack4提升180%编译速度
    [转] 默认分包策略
  • 原文地址:https://www.cnblogs.com/surpass123/p/13387758.html
Copyright © 2020-2023  润新知