189 8069 5689

免费课页面前端搭建和后端课程分类接口、课程群查接口设计

免费课页面前端搭建

点击查看代码





课程分类接口实现

serializes.py

创新互联主要从事网站建设、网站制作、网页设计、企业做网站、公司建网站等业务。立足成都服务杞县,十年网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:13518219792

from rest_framework import serializers
from . import models

class CourseCategorySerialize(serializers.ModelSerializer):
    class Meta:
        model = models.CourseCategory
        fields = ['id','name']

views.py

from django.shortcuts import render
from rest_framework.viewsets import GenericViewSet
from rest_framework.mixins import ListModelMixin
from . import models
from . import serializes


# Create your views here.
class CourseCategory(GenericViewSet,ListModelMixin):
    queryset = models.CourseCategory.objects.filter(is_delete=False,is_show=True).order_by('orders')
    serializer_class = serializes.CourseCategorySerialize

urls.py

from django.urls import path,re_path,include
from course import views
from rest_framework.routers import SimpleRouter

router = SimpleRouter()
router.register('categories',views.CourseCategory,'category')

urlpatterns = [
    path('',include(router.urls)),
]

课程群查接口搭建

serializes.py

from rest_framework import serializers
from . import models

class CourseCategorySerialize(serializers.ModelSerializer):
    class Meta:
        model = models.CourseCategory
        fields = ['id','name']


class TeacherSerialize(serializers.ModelSerializer):
    class Meta:
        model = models.Teacher
        fields = ('name', 'role_name', 'title', 'signature', 'image', 'brief')


class CourseModelSerialize(serializers.ModelSerializer):
    # 由于课程也需要teacher表相关的信息,所以可以采用子序列化的方式,获取teacher相关的信息
    teacher = TeacherSerialize()
    class Meta:
        model = models.Course
        fields = ['id',
                  'name',
                  'price',
                  'course_img',
                  'brief',
                  'attachment_path',
                  'pub_sections',
                  'students',
                  'period',
                  'sections',
                  'teacher',
                  'course_type_name', # 以下字段是models里的方法名
                  'level_name',
                  'status_name',
                  'section_list'
                  ]

views.py

from rest_framework.filters import OrderingFilter,SearchFilter
from django_filters.rest_framework import DjangoFilterBackend

class CourseView(GenericViewSet,ListModelMixin):
    queryset = models.Course.objects.filter(is_delete=False,is_show=True).order_by('orders')
    serializer_class = serializes.CourseModelSerialize
    filter_backends = [OrderingFilter,DjangoFilterBackend]
    ordering_fields = ['id','price','students']
    filterset_fields = ['course_category',]

分享名称:免费课页面前端搭建和后端课程分类接口、课程群查接口设计
转载注明:http://jkwzsj.com/article/dsogdio.html

其他资讯