项目课程模块(二)
一、课程详情接口
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、接口访问效果
三、评论及常见问题接口
1、添加评论和常见问题url
LuffyCity/Course/urls.py文件中添加课程评论和常见问题url:
from django.urls import path from .views import * 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()), # 课程章节 path('comment/<int:pk>', CourseCommentView.as_view()), # 课程评论 path('question/<int:pk>', QuestionView.as_view()) # 常见问题 ]
2、课程评论和常见问题序列化器
在LuffyCity/Course/serializers.py文件中添加如下内容:
class CourseCommentSerializer(serializers.ModelSerializer): # 评论表保存的account是foreignkey,需要处理 account = serializers.CharField(source="account.username") # 如果需要展示用户更多信息,可设置为SerializerMethodField做处理 class Meta: model = models.Comment fields = ["id", "account", "content", "date"] class QuestionSerializer(serializers.ModelSerializer): class Meta: model = models.OftenAskedQuestion fields = ["id", "question", "answer"]
3、课程评论和常见问题视图
class CourseCommentView(APIView): def get(self, request, pk): # 通过课程id找到课程所有的评论——先拿到课程对象,再通过课程对象获取所有的评论 queryset = models.Course.objects.filter(id=pk).first().course_comments.all() # 序列化 ser_obj = CourseCommentSerializer(queryset, many=True) # 返回 return Response(ser_obj.data) class QuestionView(APIView): def get(self, request, pk): # 通过课程找到所有的课程常见问题 queryset = models.Course.objects.filter(id=pk).first().often_ask_questions.all() # 反向查询拿到常见问题 # 序列化 ser_obj = QuestionSerializer(queryset, many=True) # 返回 return Response(ser_obj.data)
4、接口访问效果
(1)评论接口
(2)常见问题接口