一、课程详情接口 1、添加课程详情url LuffyCity/Course/urls.py文件中添加课程详情url,携带course_id: from django.urls import path from .views import CategoryView, CourseView, CourseDetailViewurlpatterns = [ path( ‘
一、课程详情接口
1、添加课程详情url
LuffyCity/Course/urls.py文件中添加课程详情url,携带course_id:
from django.urls import path from .views import CategoryView, CourseView, CourseDetailView urlpatterns = [ path(‘category‘, CategoryView.as_view()), # 课程分类 path(‘list‘, CourseView.as_view()), # 查看课程 path(‘detail/<int:pk>‘, CourseDetailView.as_view()) # 查看课程详情,携带course_id ]
2、添加课程详情序列化器
在LuffyCity/Course/serializers.py文件中添加如下内容:
class CourseDetailSerializer(serializers.ModelSerializer): level = serializers.CharField(source="course.get_level_display") # 难度 study_num = serializers.IntegerField(source="course.study_num") # 学习人数 recommend_courses = serializers.SerializerMethodField() # 推荐课程 teachers = serializers.SerializerMethodField() # 课程老师 price_policy = serializers.SerializerMethodField() # 价格策略 course_outline = serializers.SerializerMethodField() # 课程大纲 def get_recommend_courses(self, obj): # 获取所有的推荐课程,主要是获取两个字段:id、title return [{"id": course.id, "title": course.title} for course in obj.recommend_courses.all()] def get_teachers(self, obj): # 获取课程老师id和名字 return [{"id": teacher.id, "name": teacher.name} for teacher in obj.teachers.all()] def get_price_policy(self, obj): # 获取价格策略,获取价格策略周期(中文显示)、价格信息 return [ { "id": price.id, "valid_price_display": price.get_valid_period_display(), # 价格周期添加‘_display‘,可不显示数字,显示中文 "price": price.price } for price in obj.course.price_policy.all()] def get_course_outline(self, obj): # 获取课程大纲,课程大纲和课程详情表是外键绑定关系 return [ { "id": outline.id, "title": outline.title, "content": outline.content } for outline in obj.course_outline.all().order_by("order")] # 拿到所有大纲,以order排序 class Meta: model = models.CourseDetail fields = ["id", "hours", "summary", "level", "study_num", "recommend_courses", "teachers", "price_policy", "course_outline", ]
3、添加课程详情视图
在LuffyCity/Course/views.py中添加如下内容:
from .serializers import CategorySerializer, CourseSerializer, CourseDetailSerializer # 引入序列化器 class CourseDetailView(APIView): def get(self, request, pk): # 根据pk获取课程详情对象 course_detail_obj = models.CourseDetail.objects.filter(course__id=pk).first() if not course_detail_obj: return Response({"code": 1001, "error": "查询的课程详情不存在"}) # 序列化课程详情 ser_obj = CourseDetailSerializer(course_detail_obj) # 返回 return Response(ser_obj.data)
4、接口访问效果
使用admin组件添加课程详情数据,再使用postman调用接口访问数据,效果如下所示:
二、课程章节接口
1、添加课程章节url
LuffyCity/Course/urls.py文件中添加课程章节url:
from django.urls import path from .views import CategoryView, CourseView, CourseDetailView, CourseChapterView urlpatterns = [ path(‘category‘, CategoryView.as_view()), # 课程分类 path(‘list‘, CourseView.as_view()), # 查看课程 path(‘detail/<int:pk>‘, CourseDetailView.as_view()), # 查看课程详情,携带course_id path(‘chapter/<int:pk>‘, CourseChapterView.as_view()) # 课程章节 ]
2、课程章节序列化器
在LuffyCity/Course/serializers.py文件中添加如下内容:
class CourseChapterSeriallizer(serializers.ModelSerializer): sections = serializers.SerializerMethodField() def get_sections(self, obj): return [ { "id": section.id, "title": section.title, "free_trail": section.free_trail } for section in obj.course_sections.all().order_by("section_order")] class Meta: model = models.CourseChapter fields = ["id", "title", "sections"]
3、课程章节视图
class CourseChapterView(APIView): def get(self, request, pk): # ["第一章": {课时一, 课时二}] queryset = models.CourseChapter.objects.filter(course_id=pk).all().order_by("chapter") # 序列化章节对象 ser_obj = CourseChapterSeriallizer(queryset, many=True) # 返回 return Response(ser_obj.data)
4、接口访问效果