【课程板块表分析】
1 课程分类表:一个课程分类下,有多门课程
-id
-分类名
2 课程表
-课程有多种类型---》多个表,还是一个表--》
-不同课程字段不一样--》所以不建议放到一个表中---》
-免费
-实战
-轻课
-不同课程,使用不同表来存
-开发时候,不同人,开发不同模块,不会冲突
-实战课板块:实战课表---》其它表,其它板块写
3 老师表:跟课程一对多
4 章节:章节和课程一对多
5 课时表:课时跟章节一对多
===========================================================================
【创建表】
1 from django.db import models 2 from utils.common_model import BaseModel 3 4 5 # Create your models here. 6 7 # 课程分类表,实战课表,老师表,章节表,课时表 8 9 class CourseCategory(BaseModel): 10 """课程分类表""" 11 name = models.CharField(max_length=64, unique=True, verbose_name="分类名称") 12 13 class Meta: 14 db_table = "luffy_course_category" 15 verbose_name = "分类" 16 verbose_name_plural = verbose_name 17 18 def __str__(self): 19 return "%s" % self.name 20 21 22 class Course(BaseModel): 23 """课程表""" 24 course_type = ( 25 (0, '付费'), 26 (1, 'VIP专享'), 27 ) 28 level_choices = ( 29 (0, '初级'), 30 (1, '中级'), 31 (2, '高级'), 32 ) 33 status_choices = ( 34 (0, '上线'), 35 (1, '下线'), 36 (2, '预上线'), 37 ) 38 name = models.CharField(max_length=128, verbose_name="课程名称") 39 course_img = models.ImageField(upload_to="courses", max_length=255, verbose_name="封面图片", blank=True, null=True) 40 course_type = models.SmallIntegerField(choices=course_type, default=0, verbose_name="付费类型") 41 brief = models.TextField(max_length=2048, verbose_name="详情介绍", null=True, blank=True) 42 level = models.SmallIntegerField(choices=level_choices, default=0, verbose_name="难度等级") 43 pub_date = models.DateField(verbose_name="发布日期", auto_now_add=True) 44 period = models.IntegerField(verbose_name="建议学习周期(day)", default=7) 45 attachment_path = models.FileField(upload_to="attachment", max_length=128, verbose_name="课件路径", blank=True, 46 null=True) 47 status = models.SmallIntegerField(choices=status_choices, default=0, verbose_name="课程状态") 48 students = models.IntegerField(verbose_name="学习人数", default=0) 49 sections = models.IntegerField(verbose_name="总课时数量", default=0) 50 pub_sections = models.IntegerField(verbose_name="课时更新数量", default=0) 51 price = models.DecimalField(max_digits=6, decimal_places=2, verbose_name="课程原价", default=0) 52 53 # on_delete 可以选择字段 54 # db_constraint 55 teacher = models.ForeignKey("Teacher", on_delete=models.DO_NOTHING, null=True, blank=True, verbose_name="授课老师") 56 course_category = models.ForeignKey("CourseCategory", on_delete=models.SET_NULL, db_constraint=False, null=True, 57 blank=True, verbose_name="课程分类") 58 59 class Meta: 60 db_table = "luffy_course" 61 verbose_name = "课程" 62 verbose_name_plural = "课程" 63 64 def __str__(self): 65 return "%s" % self.name 66 67 68 class Teacher(BaseModel): 69 """老师表""" 70 role_choices = ( 71 (0, '讲师'), 72 (1, '导师'), 73 (2, '班主任'), 74 ) 75 name = models.CharField(max_length=32, verbose_name="导师名") 76 role = models.SmallIntegerField(choices=role_choices, default=0, verbose_name="导师身份") 77 title = models.CharField(max_length=64, verbose_name="职位、职称") 78 signature = models.CharField(max_length=255, verbose_name="导师签名", help_text="导师签名", blank=True, null=True) 79 image = models.ImageField(upload_to="teacher", null=True, verbose_name="导师封面") 80 brief = models.TextField(max_length=1024, verbose_name="导师描述") 81 82 class Meta: 83 db_table = "luffy_teacher" 84 verbose_name = "导师" 85 verbose_name_plural = verbose_name 86 87 def __str__(self): 88 return "%s" % self.name 89 90 91 class CourseChapter(BaseModel): 92 """章节表""" 93 # related_name 94 course = models.ForeignKey("Course", related_name='coursechapters', on_delete=models.CASCADE, 95 verbose_name="课程名称") 96 chapter = models.SmallIntegerField(verbose_name="第几章", default=1) 97 name = models.CharField(max_length=128, verbose_name="章节标题") 98 summary = models.TextField(verbose_name="章节介绍", blank=True, null=True) 99 pub_date = models.DateField(verbose_name="发布日期", auto_now_add=True) 100 101 class Meta: 102 db_table = "luffy_course_chapter" 103 verbose_name = "章节" 104 verbose_name_plural = verbose_name 105 106 def __str__(self): 107 return "%s:(第%s章)%s" % (self.course, self.chapter, self.name) 108 109 110 class CourseSection(BaseModel): 111 """课时表""" 112 section_type_choices = ( 113 (0, '文档'), 114 (1, '练习'), 115 (2, '视频') 116 ) 117 chapter = models.ForeignKey("CourseChapter", related_name='coursesections', on_delete=models.CASCADE, 118 verbose_name="课程章节") 119 name = models.CharField(max_length=128, verbose_name="课时标题") 120 orders = models.PositiveSmallIntegerField(verbose_name="课时排序") 121 section_type = models.SmallIntegerField(default=2, choices=section_type_choices, verbose_name="课时种类") 122 section_link = models.CharField(max_length=255, blank=True, null=True, verbose_name="课时链接", 123 help_text="若是video,填vid,若是文档,填link") 124 duration = models.CharField(verbose_name="视频时长", blank=True, null=True, max_length=32) # 仅在前端展示使用 125 pub_date = models.DateTimeField(verbose_name="发布时间", auto_now_add=True) 126 free_trail = models.BooleanField(verbose_name="是否可试看", default=False) 127 128 class Meta: 129 db_table = "luffy_course_section" 130 verbose_name = "课时" 131 verbose_name_plural = verbose_name 132 133 def __str__(self): 134 return "%s-%s" % (self.chapter, self.name) 135 136 -------------------------------------------------------------- 137 admin中注册表 138 from django.contrib import admin 139 from .models import * 140 141 # Register your models here. 142 143 admin.site.register(CourseCategory) 144 admin.site.register(Course) 145 admin.site.register(Teacher) 146 admin.site.register(CourseChapter) 147 admin.site.register(CourseSection) 148 149 -------------------------------------------------------------- 150 迁移表 151 python manage.py makemigrations 152 python manage.py migrate创建课程表模型
。
。
【ForeignKey中参数】
1 to:跟哪个表管理,需要配合to_field,如果不写,会关联主键 2 to_field=None --------------------------- 3 on_delete:当这条记录删除时--》外键 -CASCADE:级联删除:用户和用户详情,课程和章节,章节和课时 -SET_NULL:关联的删除,这个字段设为空,但是需要配合:null=True -SET_DEFAULT:关联的删除,这个字段设为默认值,但是需要配合:default=xx -SET(函数内存地址):关联的删除,会触发这个函数执行 --------------------- # orm查询,正向和反向 -基于对象的跨表查询 -book.publish --> 正向 -publish.book_set.all()-->反向 -基于双下划线的跨表查询 -book__publish_name-->正向 -publish__book_name-->反向 -正向按字段 -反向:按表名小写(是否带set取决于是否是多),基于双下划线的都是表名小写 4 related_name=None:基于对象跨表查,反向查询的名字 (原来:按表名小写-是否带set取决于是否是多),现在按这个字段 -原来:course.coursechapter_set.all() -现在course.coursechapters.all() 5 related_query_name=None 基于下划线跨表查,反向查询的名字,现在按这个字段 publish__指定的字段_name 6 db_constraint=False 不建立强外键关系,默认是True -强外键--》er图上有条线--》关联操作时,会有限制,会有约束 -会消耗性能 -实际工作中,不建立强外键,但是有外键关系--》er图上没有有条线--》orm关联操作一样用 -以后存数据,删除数据,就不会检索关联表,性能高 -可能会录入 脏数据 :程序层面控制
。
。
【课程相关数据录入】
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, '2022-07-14 13:44:19.661327', '2022-07-14 13:46:54.246271', 'Alex', 1, '老男孩Python教学总监', '金角大王', 'teacher/alex_icon.png', '老男孩教育CTO & CO-FOUNDER 国内知名PYTHON语言推广者 51CTO学院2016\2017年度最受学员喜爱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, '2022-07-14 13:45:25.092902', '2022-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, '2022-07-14 13:46:21.997846', '2022-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, '2022-07-14 13:40:58.690413', '2022-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, '2022-07-14 13:41:08.249735', '2022-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, '2022-07-14 13:54:33.095201', '2022-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, '2022-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, '2022-07-14 13:56:05.051103', '2022-07-14 13:56:05.051142', 'Python项目实战', 'courses/mjj_python.png', 0, '', 1, '2022-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, '2022-07-14 13:57:21.190053', '2022-07-14 13:57:21.190095', 'Linux系统基础5周入门精讲', 'courses/lyy_linux.png', 0, '', 0, '2022-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, '2022-07-14 13:58:34.867005', '2022-07-14 14:00:58.276541', 1, '计算机原理', '', '2022-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, '2022-07-14 13:58:48.051543', '2022-07-14 14:01:22.024206', 2, '环境搭建', '', '2022-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, '2022-07-14 13:59:09.878183', '2022-07-14 14:01:40.048608', 1, '项目创建', '', '2022-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, '2022-07-14 13:59:37.448626', '2022-07-14 14:01:58.709652', 1, 'Linux环境创建', '', '2022-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, '2022-07-14 14:02:33.779098', '2022-07-14 14:02:33.779135', '计算机原理上', 1, 2, NULL, NULL, '2022-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, '2022-07-14 14:02:56.657134', '2022-07-14 14:02:56.657173', '计算机原理下', 2, 2, NULL, NULL, '2022-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, '2022-07-14 14:03:20.493324', '2022-07-14 14:03:52.329394', '环境搭建上', 1, 2, NULL, NULL, '2022-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, '2022-07-14 14:03:36.472742', '2022-07-14 14:03:36.472779', '环境搭建下', 2, 2, NULL, NULL, '2022-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, '2022-07-14 14:04:19.338153', '2022-07-14 14:04:19.338192', 'web项目的创建', 1, 2, NULL, NULL, '2022-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, '2022-07-14 14:04:52.895855', '2022-07-14 14:04:52.895890', 'Linux的环境搭建', 1, 2, NULL, NULL, '2022-07-14 14:04:52.895942', 1, 4); -- 课程 INSERT INTO `luffy_course` VALUES (4, '2022-04-28 12:06:36.564933', '2022-04-28 12:36:04.812789', 0, 1, 4, 'DRF从入门到放弃', 'courses/drf.png', 0, 'drf很牛逼', 4, '2022-04-28', 7, '', 0, 399, 0, 0, 77.00, 1, 1); INSERT INTO `luffy_course` VALUES (5, '2022-04-28 12:35:44.319734', '2022-04-28 12:35:44.319757', 0, 1, 5, 'Go语言从入门到入坑', 'courses/msbd.png', 0, 'Go语言从入门到入坑Go语言从入门到入坑Go语言从入门到入坑Go语言从入门到入坑', 0, '2022-04-28', 20, '', 0, 30, 200, 100, 66.00, 3, 1); INSERT INTO `luffy_course` VALUES (6, '2022-04-28 12:39:55.562716', '2022-04-28 12:39:55.562741', 0, 1, 6, 'Go语言微服务', 'courses/celery.png', 0, 'Go语言微服务Go语言微服务Go语言微服务Go语言微服务', 4, '2022-04-28', 7, '', 0, 122, 0, 0, 299.00, 3, 2); -- 分类 INSERT INTO `luffy_course_category` VALUES (3, '2022-04-28 12:07:33.314057', '2022-04-28 12:07:33.314088', 0, 1, 3, 'Go语言'); -- 章节 INSERT INTO `luffy_course_chapter` VALUES (5, '2022-04-28 12:08:36.679922', '2022-04-28 12:08:36.680014', 0, 1, 2, 2, 'Linux5周第二章', 'Linux5周第二章Linux5周第二章Linux5周第二章Linux5周第二章Linux5周第二章', '2022-04-28', 3); INSERT INTO `luffy_course_chapter` VALUES (6, '2022-04-28 12:09:19.324504', '2022-04-28 12:09:19.324533', 0, 1, 2, 2, 'py实战项目第二章', 'py实战项目第二章py实战项目第二章py实战项目第二章py实战项目第二章', '2022-04-28', 2); INSERT INTO `luffy_course_chapter` VALUES (7, '2022-04-28 12:09:32.532905', '2022-04-29 10:11:57.546455', 0, 1, 3, 3, 'py实战项目第三章', 'py实战项目第三章py实战项目第三章py实战项目第三章', '2022-04-28', 2); INSERT INTO `luffy_course_chapter` VALUES (8, '2022-04-28 12:09:55.496622', '2022-04-28 12:09:55.496686', 0, 1, 1, 1, 'drf入门1', 'drf入门1drf入门1drf入门1', '2022-04-28', 4); INSERT INTO `luffy_course_chapter` VALUES (9, '2022-04-28 12:10:08.490618', '2022-04-28 12:10:08.490642', 0, 1, 2, 2, 'drf入门2', 'drf入门drf入门1drf入门1drf入门1drf入门1', '2022-04-28', 4); INSERT INTO `luffy_course_chapter` VALUES (10, '2022-04-28 12:10:22.088684', '2022-04-28 12:10:22.088710', 0, 1, 3, 3, 'drf入门3', 'drf入门1drf入门1drf入门1drf入门1drf入门1drf入门1', '2022-04-28', 4); INSERT INTO `luffy_course_chapter` VALUES (11, '2022-04-28 12:10:33.564141', '2022-04-28 12:10:33.564177', 0, 1, 4, 4, 'drf入门4', 'drf入门1drf入门1drf入门1drf入门1', '2022-04-28', 4); INSERT INTO `luffy_course_chapter` VALUES (12, '2022-04-28 12:10:43.242918', '2022-04-28 12:10:43.242947', 0, 1, 5, 5, 'drf入门5', 'drf入门1drf入门1drf入门1drf入门1', '2022-04-28', 4); INSERT INTO `luffy_course_chapter` VALUES (13, '2022-04-28 12:36:58.508995', '2022-04-28 12:36:58.509020', 0, 1, 1, 1, 'go第一章', 'go第一章', '2022-04-28', 5); INSERT INTO `luffy_course_chapter` VALUES (14, '2022-04-28 12:37:08.588265', '2022-04-28 12:37:08.588287', 0, 1, 2, 2, 'go第二章', 'go第一章go第一章go第一章', '2022-04-28', 5); INSERT INTO `luffy_course_chapter` VALUES (15, '2022-04-28 12:37:19.219405', '2022-04-28 12:37:19.219426', 0, 1, 3, 3, 'go第三章', 'go第一章go第一章go第一章', '2022-04-28', 5); INSERT INTO `luffy_course_chapter` VALUES (16, '2022-04-28 12:40:11.445750', '2022-04-28 12:40:11.445774', 0, 1, 1, 1, '微服务第一章', '微服务第一章', '2022-04-28', 6); INSERT INTO `luffy_course_chapter` VALUES (17, '2022-04-28 12:40:22.811647', '2022-04-28 12:40:22.811670', 0, 1, 2, 2, '微服务第二章', '微服务第二章微服务第二章微服务第二章', '2022-04-28', 6); -- 课时 INSERT INTO `luffy_course_section` VALUES (7, '2022-04-28 12:12:01.304920', '2022-04-28 12:12:01.304994', 0, 1, '文件操作', 2, 2, NULL, NULL, '2022-04-28 12:12:01.305074', 0, 5); INSERT INTO `luffy_course_section` VALUES (8, '2022-04-28 12:12:11.287759', '2022-04-28 12:12:11.287884', 0, 1, '软件操作', 2, 2, NULL, NULL, '2022-04-28 12:12:11.288079', 0, 5); INSERT INTO `luffy_course_section` VALUES (9, '2022-04-28 12:12:26.326077', '2022-04-28 12:12:26.326112', 0, 1, '请求响应', 1, 2, NULL, NULL, '2022-04-28 12:12:26.326174', 0, 8); INSERT INTO `luffy_course_section` VALUES (10, '2022-04-28 12:12:36.364356', '2022-04-28 12:12:36.364391', 0, 1, '序列化类', 2, 2, NULL, NULL, '2022-04-28 12:12:36.364446', 0, 8); INSERT INTO `luffy_course_section` VALUES (11, '2022-04-28 12:12:48.306119', '2022-04-28 12:12:48.306187', 0, 1, '三大认证', 1, 2, NULL, NULL, '2022-04-28 12:12:48.306396', 0, 9); INSERT INTO `luffy_course_section` VALUES (12, '2022-04-28 12:13:06.882558', '2022-04-28 12:13:06.882620', 0, 1, '认证', 2, 2, NULL, NULL, '2022-04-28 12:13:06.882826', 0, 9); INSERT INTO `luffy_course_section` VALUES (13, '2022-04-28 12:13:15.799043', '2022-04-28 12:13:15.799084', 0, 1, 'jwt认证', 1, 2, NULL, NULL, '2022-04-28 12:13:15.799146', 0, 10); INSERT INTO `luffy_course_section` VALUES (14, '2022-04-28 12:13:27.852981', '2022-04-28 12:13:27.853011', 0, 1, 'jwt认证2', 3, 2, NULL, NULL, '2022-04-28 12:13:27.853066', 0, 10); INSERT INTO `luffy_course_section` VALUES (15, '2022-04-28 12:13:37.292779', '2022-04-28 12:13:37.292806', 0, 1, '后台管理', 1, 2, NULL, NULL, '2022-04-28 12:13:37.292855', 0, 11); INSERT INTO `luffy_course_section` VALUES (16, '2022-04-28 12:13:51.194585', '2022-04-28 12:13:51.194612', 0, 1, '后台管理2', 2, 2, NULL, NULL, '2022-04-28 12:13:51.194660', 0, 11); INSERT INTO `luffy_course_section` VALUES (17, '2022-04-28 12:14:05.334836', '2022-04-28 12:14:05.334902', 0, 1, 'rbac1', 1, 2, NULL, NULL, '2022-04-28 12:14:05.335053', 0, 12); INSERT INTO `luffy_course_section` VALUES (18, '2022-04-28 12:14:14.039605', '2022-04-28 12:14:14.039770', 0, 1, 'rbac2', 2, 2, NULL, NULL, '2022-04-28 12:14:14.039895', 0, 12); INSERT INTO `luffy_course_section` VALUES (19, '2022-04-28 12:37:34.682049', '2022-04-28 12:37:34.682072', 0, 1, '环境搭建', 1, 2, NULL, NULL, '2022-04-28 12:37:34.682116', 0, 13); INSERT INTO `luffy_course_section` VALUES (20, '2022-04-28 12:37:46.317414', '2022-04-28 12:37:46.317440', 0, 1, '第一个helloworld', 2, 2, NULL, NULL, '2022-04-28 12:37:46.317483', 0, 13); INSERT INTO `luffy_course_section` VALUES (21, '2022-04-28 12:37:54.200236', '2022-04-28 12:37:54.200257', 0, 1, '变量定义', 1, 2, NULL, NULL, '2022-04-28 12:37:54.200297', 0, 14); INSERT INTO `luffy_course_section` VALUES (22, '2022-04-28 12:38:03.465663', '2022-04-28 12:38:03.465686', 0, 1, '常量', 2, 2, NULL, NULL, '2022-04-28 12:38:03.465731', 0, 14); INSERT INTO `luffy_course_section` VALUES (23, '2022-04-28 12:38:13.144613', '2022-04-28 12:38:13.144636', 0, 1, 'go结构体', 1, 2, NULL, NULL, '2022-04-28 12:38:13.144679', 0, 15); INSERT INTO `luffy_course_section` VALUES (24, '2022-04-28 12:38:26.312273', '2022-04-28 12:38:26.312306', 0, 1, 'go接口', 2, 2, NULL, NULL, '2022-04-28 12:38:26.312380', 0, 15); INSERT INTO `luffy_course_section` VALUES (25, '2022-04-28 12:40:36.531566', '2022-04-29 10:12:42.497098', 0, 1, '微服务第一章第一课时', 1, 2, NULL, NULL, '2022-04-28 12:40:36.531625', 1, 16); INSERT INTO `luffy_course_section` VALUES (26, '2022-04-28 12:40:45.120568', '2022-04-28 12:41:14.341536', 0, 1, '微服务第一章第二课时', 2, 2, NULL, NULL, '2022-04-28 12:40:45.120627', 0, 16); INSERT INTO `luffy_course_section` VALUES (27, '2022-04-28 12:40:57.477026', '2022-04-28 12:40:57.477048', 0, 1, '微服务第二章第一课时', 1, 2, NULL, NULL, '2022-04-28 12:40:57.477088', 0, 17); INSERT INTO `luffy_course_section` VALUES (28, '2022-04-28 12:41:04.673613', '2022-04-28 12:41:04.673634', 0, 1, '微服务第二章第二课时', 2, 2, NULL, NULL, '2022-04-28 12:41:04.673673', 0, 17);数据录入
。
。
【课程分类接口】
视图类
1 from django.shortcuts import render 2 from rest_framework.viewsets import GenericViewSet 3 4 from utils.common_mixin import APIListModelMixin 5 from .models import CourseCategory 6 from .serializer import CourseCategorySerializer 7 8 9 # Create your views here. 10 # 课程分类接口,因为是自动生成的路由,要继承APIListModelMixin 11 class CourseCategoryView(GenericViewSet, APIListModelMixin): 12 queryset = CourseCategory.objects.all().filter(is_delete=False, is_show=True).order_by('-orders') 13 serializer_class = CourseCategorySerializer
序列化类
1 from .models import CourseCategory 2 from rest_framework import serializers 3 4 5 class CourseCategorySerializer(serializers.ModelSerializer): 6 class Meta: 7 model = CourseCategory 8 fields = ['id', 'name']
路由
1 from django.urls import path 2 from .views import CourseCategoryView 3 from rest_framework.routers import SimpleRouter 4 5 router = SimpleRouter() 6 router.register('category', CourseCategoryView, 'category') 7 8 urlpatterns = [ 9 ] 10 urlpatterns += router.urls
。
。
【课程列表接口】
1 查询所有课程,带过滤,带分页,关联表数据也要返回
-返回课时:如果总课时数,大于4,就返回4条,如果小于4,有多少返回多少
视图类
1 class CourseView(GenericViewSet,APIListModelMixin): 2 queryset = Course.objects.all().filter(is_delete=False, is_show=True).order_by('-orders') 3 serializer_class = CourseSerializer
序列化类
1 class TeacherSerializer(serializers.ModelSerializer): 2 class Meta: 3 model = Teacher 4 fields = [ 5 'name', 6 'role_name', # 重写 7 'title', 8 'signature', 9 'image', 10 'brief' 11 ] 12 13 14 # 课程序列化类 15 class CourseSerializer(serializers.ModelSerializer): 16 teacher = TeacherSerializer() # 子序列化 17 18 class Meta: 19 model = Course 20 fields = [ 21 'id', 22 'name', 23 'course_img', 24 'price', 25 'students', 26 'pub_sections', # 发布多少课时 27 'sections', # 列表页面不显示,详情接口会显示 28 'period', # 建议学习周期 29 'brief', # 列表页面不显示,详情接口会显示 30 'attachment_path', # 文档地址 31 # choice字段,定制返回格式--》表模型中写 32 'course_type_name', 33 'level_name', 34 'status_name', 35 # 关联表 36 'teacher', # teacher 所有数据---》子序列化 37 'section_list' # 返回课时:如果总课时数,大于4,就返回4条,如果小于4,有多少返回多少--表模型 38 ]
表模型(有改动)
1 # 课程表 2 3 @property 4 def course_type_name(self): 5 return self.get_course_type_display() 6 7 @property 8 def level_name(self): 9 return self.get_level_display() 10 11 @property 12 def status_name(self): 13 return self.get_status_display() 14 15 def section_list(self): 16 # 如果总课时数,大于4,就返回4条,如果小于4,有多少返回多少 17 l = [] 18 # 先循环章节[课程拿到所有章节:反向], 19 for course_chapter in self.coursechapters.all(): 20 # 再循环课时【从章节拿到课时:反】 21 for course_section in course_chapter.coursesections.all(): 22 l.append({ 23 'name': course_section.name, 24 'section_link': course_section.section_link, 25 'duration': course_section.duration, 26 'free_trail': course_section.free_trail, 27 }) 28 if len(l) == 4: 29 return l 30 return l 31 32 33 ==================================== 34 老师表 35 36 def role_name(self): 37 return self.get_role_display()
路由
router.register('actual', CourseView, 'actual')
。
。
【分页和过滤】
分页类
pip install django_filter,注册
1 from rest_framework.pagination import PageNumberPagination 2 3 4 class CommonPageNumberPagination(PageNumberPagination): 5 page_size = 2 6 page_query_param = 'page' 7 page_size_query_param = 'size' 8 max_page_size = 5
视图类
class CourseView(GenericViewSet, APIListModelMixin):
queryset = Course.objects.all().filter(is_delete=False, is_show=True).order_by('orders')
serializer_class = CourseSerializer
pagination_class = PageNumberPagination # 分页
# 过滤:按课程分类id号过滤
# 排序
filter_backends = [OrderingFilter, DjangoFilterBackend]
ordering_fields = ['id', 'students', 'price', ]
filterset_fields = ['course_category'] # 按分类过滤
minxin进一步封装
class APIListModelMixin(ListModelMixin): def list(self, request, *args, **kwargs): res = super().list(request, *args, **kwargs) # 分页封装 if self.paginator: return APIResponse( count=res.data.get('count'), next=res.data.get('next'), previous=res.data.get('previous'), results=res.data.get('results'), ) return APIResponse(results=res.data)
。
。
【课程详情接口】
# 1 写课程详情接口,只需要再视图类上配置 APIRetrieveModelMixin类即可
class CourseView(GenericViewSet, APIListModelMixin,APIRetrieveModelMixin)
# 2 但是存在问题是:课程详情页面的原型图上有些数据,没有返回的
-1 方案一: 再写一个序列化类,通过重写get_serializer_class,控制不同请求,使用不同序列化类
-在序列化类中使用两层子序列化
-2 方案二:再写个接口--》查询所有章节接口+按课程过滤
1 方案一 2 3 视图类 4 class CourseView(GenericViewSet, APIListModelMixin, APIRetrieveModelMixin): 5 def get_serializer_class(self): 6 if self.action == 'retrieve': 7 return CourseDetailSerializer 8 else: 9 return CourseSerializer 10 11 序列化类 12 # 课程详情序列化类 13 class CourseSectionSerializer(serializers.ModelSerializer): 14 class Meta: 15 model = CourseSection 16 fields = [ 17 'id', 18 'name', 19 'orders', 20 'section_link', 21 'duration', 22 'free_trail' 23 ] 24 25 26 class CourseChapterSerializer(serializers.ModelSerializer): 27 coursesections = CourseSectionSerializer(many=True) # 子序列化,多条,要写many=True 28 29 class Meta: 30 model = CourseChapter 31 fields = [ 32 'id', 33 'name', 34 'coursesections', 35 ] 36 37 38 class CourseDetailSerializer(serializers.ModelSerializer): 39 teacher = TeacherSerializer() # 子序列化 40 coursechapters = CourseChapterSerializer(many=True) # 子序列化,多条,要写many=True 41 42 class Meta: 43 model = Course 44 fields = [ 45 'id', 46 'name', 47 'course_img', 48 'price', # 49 'students', 50 'pub_sections', # 发布多少课时 51 'sections', 52 53 # 列表页面不显示,详情接口会显示 54 'period', # 建议学习周期 55 'brief', 56 'attachment_path', # 文档地址 57 58 # choice字段,定制返回格式--》表模型中写 59 'course_type_name', 60 'level_name', 61 'status_name', 62 # 关联表 63 'teacher', # teacher 所有数据---》子序列化 64 'coursechapters', # 所有章节 65 ] 66 67 68 ===================================== 69 方案二: 70 # 查询所有章节接口,带按课程过滤 71 class CourseChapterView(GenericViewSet, APIListModelMixin): 72 queryset = CourseChapter.objects.all().filter(is_delete=False, is_show=True) 73 serializer_class = CourseChapterSerializer 74 filter_backends = [DjangoFilterBackend] 75 filterset_fields = ['course'] # 按课程过滤
。
。
【课程列表前端】
标签:02,12,name,04,项目,28,路飞,course,2022 From: https://www.cnblogs.com/liuliu1/p/18202941