基础接口
序列化:course/serializers.py
from rest_framework import serializers
from .models import CourseCategory, Course, Teacher
class CourseCategorySerializer(serializers.ModelSerializer):
class Meta:
model = CourseCategory
fields = ["id","name"]
class TeacherSerializer(serializers.ModelSerializer):
"""课程列表的老师信息"""
class Meta:
model = Teacher
fields = ["id", "name", "role", "title", "signature", "brief", "image"]
class CourseSerializer(serializers.ModelSerializer):
"""课程列表的课程基本信息"""
teacher = TeacherSerializer()
class Meta:
model = Course
fields = ["id", "name", "course_img", "students", "sections", "pub_sections", "price", "teacher", 'section_list]
视图:course/views.py
from rest_framework.generics import ListAPIView
from .models import CourseCategory, Course
from .serializers import CourseCategorySerializer, CourseSerializer
class CourseCategoryAPIView(ListAPIView):
"""课程分类列表接口"""
queryset = CourseCategory.objects.filter(is_delete=False,is_show=True).order_by("orders")
serializer_class = CourseCategorySerializer
class CourseAPIView(ListAPIView):
"""课程列表接口"""
queryset = Course.objects.filter(is_delete=False, is_show=True).order_by("orders")
serializer_class = CourseSerializer
路由:course/urls.py
from django.urls import path,re_path
from . import views
urlpatterns = [
path("", views.CourseAPIView.as_view()),
path("category/", views.CourseCategoryAPIView.as_view()),
]
接口
http:
http:
连表接口
模型演变:course/models.py
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="course", 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="课程状态")
course_category = models.ForeignKey("CourseCategory", on_delete=models.SET_NULL, db_constraint=False, null=True, blank=True,
verbose_name="课程分类")
students = models.IntegerField(verbose_name="学习人数", default=0)
lessons = models.IntegerField(verbose_name="总课时数量", default=0)
pub_lessons = models.IntegerField(verbose_name="课时更新数量", default=0)
price = models.DecimalField(max_digits=6, decimal_places=2, verbose_name="课程原价", default=0)
teacher = models.ForeignKey("Teacher", on_delete=models.DO_NOTHING, 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 teacher_name(self):
"""返回老师姓名"""
return self.teacher.name
@property
def section_list(self):
"""课程列表的推荐课时"""
coursechapter_list = self.coursechapters.filter(is_show=True, is_delete=False).order_by("orders")
data_list = []
for coursechapter in coursechapter_list:
coursesection_list = coursechapter.coursesections.filter(is_show=True, is_delete=False).order_by("orders")
for coursesection in coursesection_list:
if len(data_list) >= 4:
break
data_list.append({
"id": coursesection.id,
"name": coursesection.name,
"free_trail": coursesection.free_trail,
})
return data_list
序列化演变:course/serializers.py
class CourseSerializer(serializers.ModelSerializer):
"""课程列表的课程基本信息"""
teacher = TeacherSerializer()
class Meta:
model = Course
fields = ["id", "name", "course_img", "students", "sections", "pub_sections", "price", "teacher", 'section_list]