34 课程模块——查询所有分类接口

 查询所有分类的接口

序列化类coursr/serializer.py

from rest_framework import serializers
from .models import CourseCategory
class CategorySerializer(serializers.ModelSerializer):
    class Meta:
        model=CourseCategory
        fields=['id','name']

视图类course/views.py

from django.shortcuts import render
from rest_framework.viewsets import GenericViewSet
from rest_framework.mixins import ListModelMixin
from .serializer import CategorySerializer
from course import models
# Create your views here.


class CourseCategoryView(GenericViewSet,ListModelMixin):
    serializer_class = CategorySerializer
    queryset = models.CourseCategory.objects.filter(is_show=True,is_delete=False).order_by('orders')

路由course/urls.py

from . import views
from rest_framework.routers import DefaultRouter
router=DefaultRouter()

router.register("category",views.CourseCategoryView,'category')

urlpatterns = [
    # path('test/',

]
urlpatterns+=router.urls

 

34 课程模块——查询所有分类接口

 

 34 课程模块——查询所有分类接口

 

 34 课程模块——查询所有分类接口

 

 34 课程模块——查询所有分类接口

 

上一篇:JsonResponse类的使用、form表单上传文件补充、CBV和FBV、HTML的模板语法之传值与过滤器


下一篇:TagHelper的下拉框