zoukankan      html  css  js  c++  java
  • 课程详情页之后台

    详情页后台

    路由:source/urls.py
    1
    2
    router.register('free', views.CourseViewSet, 'free')  # 课程
    router.register('chapters', views.ChapterViewSet, 'chapter') # 章节
    视图:source/views.py
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    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']

    # 过滤类:分类过滤、区间过滤
    filter_class = CourseFilterSet



    # 一个课程的所有章节(群查)
    classChapterViewSet(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
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    # ...

    classCourseSectionSerializer(serializers.ModelSerializer):
    classMeta:
    model = models.CourseSection
    fields = ('name', 'orders', 'section_link', 'duration', 'free_trail')


    classCourseChapterSerializer(serializers.ModelSerializer):
    coursesections = CourseSectionSerializer(many=True)
    classMeta:
    model = models.CourseChapter
    fields = ('name', 'chapter', 'summary', 'coursesections')
  • 相关阅读:
    JavaScript-4.5 事件大全,事件监听---ShinePans
    SparseArray具体解释,我说SparseArray,你说要!
    Spark Core源代码分析: RDD基础
    我的 Android 开发实战经验总结
    物联网的一种參考架构
    【LeetCode】 Rotate List 循环链表
    放苹果(整数划分变形题 水)poj1664
    ps白平衡
    jfinal对象封装Record原理
    ps通道混合器
  • 原文地址:https://www.cnblogs.com/plyc/p/14186134.html
Copyright © 2011-2022 走看看