课程详情页后台

目录
  • 详情页后台
    • 路由:source/urls.py
    • 视图:source/views.py
    • 序列化:source/serializers.py

详情页后台

路由:source/urls.py

router.register('free', views.CourseViewSet, 'free')  # 课程
router.register('chapters', views.ChapterViewSet, 'chapter')  # 章节

视图:source/views.py

from rest_framework.viewsets import GenericViewSet
from rest_framework.mixins import ListModelMixin, RetrieveModelMixin
from . import models, serializers

# 课程群查
from . import pagination
from rest_framework.filters import SearchFilter, OrderingFilter
from django_filters.rest_framework import DjangoFilterBackend
from .filters import CourseFilterSet
class CourseViewSet(GenericViewSet, ListModelMixin, RetrieveModelMixin):
    queryset = models.Course.objects.filter(is_delete=False, is_show=True).all()
    serializer_class = serializers.CourseSerializer

    pagination_class = pagination.PageNumberPagination
    filter_backends = [SearchFilter, OrderingFilter, DjangoFilterBackend]
    search_fields = ['name', 'id', 'brief']
    ordering_fields = ['id', 'price', 'students']
    filter_class = CourseFilterSet
    
    
# 一个课程的所有章节(群查)
class ChapterViewSet(GenericViewSet, ListModelMixin):
    queryset = models.CourseChapter.objects.filter(is_delete=False, is_show=True).all()
    serializer_class = serializers.CourseChapterSerializer

    # 基于课程分类条件下的查询
    filter_backends = [DjangoFilterBackend]
    filter_fields = ['course']

序列化:source/serializers.py

# ...

class CourseSectionSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.CourseSection
        fields = ('name', 'orders', 'section_link', 'duration', 'free_trail')


class CourseChapterSerializer(serializers.ModelSerializer):
    coursesections = CourseSectionSerializer(many=True)
    class Meta:
        model = models.CourseChapter
        fields = ('name', 'chapter', 'summary', 'coursesections')

你可能感兴趣的:(课程详情页后台)