查询所有分类的接口
序列化类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