• 路飞-课程详情页后台


    详情页后台

    路由:source/urls.py
    re_path("(?P<pk>d+)/", views.CourseRetrieveAPIView.as_view()),
    path("chapters/", views.CourseChapterListAPIView.as_view()),
    
    视图:source/views.py
    from .models import Course
    from .models import CourseChapter
    from rest_framework.generics import ListAPIView
    from rest_framework.generics import RetrieveAPIView
    from . import serializers
    from django_filters.rest_framework.backends import DjangoFilterBackend
    
    class CourseRetrieveAPIView(RetrieveAPIView):
        """课程详情信息"""
        queryset = Course.objects.filter(is_delete=False, is_show=True)
        serializer_class = serializers.CourseRetrieveModelSSerializer
    
    class CourseChapterListAPIView(ListAPIView):
        """课程详情信息"""
        queryset = CourseChapter.objects.filter(is_delete=False, is_show=True).order_by("chapter")
        serializer_class = serializers.CourseChapterModelSerializer
        filter_backends = [DjangoFilterBackend]
        filter_fields = ['course', ]
    
    序列化:source/serializers.py
    from . import models
    from rest_framework.serializers import ModelSerializer
    class CourseRetrieveModelSSerializer(ModelSerializer):
        # 课程详情的序列化器
        teacher = TeacherSerializer()
        class Meta:
            model = models.Course
            fields = ["id", "name", "course_img", "students", "sections", "pub_sections", "price", "teacher", "level_name"]
    
    class CourseSessionModelSerializer(ModelSerializer):
        class Meta:
            model = models.CourseSection
            fields = ["id", "name", "duration", "free_trail", "orders"]
    
    class CourseChapterModelSerializer(ModelSerializer):
        coursesections = CourseSessionModelSerializer(many=True)
    
        class Meta:
            model = models.CourseChapter
            fields = ["chapter", "name", "summary", "coursesections"]
    
    视图字段:source/model.py
    class Course(BaseModel):
        # ...
        @property
        def level_name(self):
            # 难度名
            return self.level_choices[self.level][1]
    
  • 相关阅读:
    关于MATLAB处理大数据坐标文件2017527
    关于MATLAB处理大数据坐标文件2017526
    关于MATLAB处理大数据坐标文件
    Python入门(2)
    Python入门
    [leetcode] 349. Intersection of Two Arrays 解题报告
    [leetcode] 283. Move Zeroes 解题报告
    [leetcode] 389. Find the Difference 解题报告
    [leetcode] 104. Maximum Depth of Binary Tree
    [leetcode] 258. Add Digits
  • 原文地址:https://www.cnblogs.com/DcentMan/p/11844751.html
Copyright © 2020-2023  润新知