路由:course/urls.py
router.register('coursesearch', views.SearchCourseViewSet, 'coursesearch') # 搜索课程
视图:course/views.py
from rest_framework.viewsets import GenericViewSet from rest_framework.mixins import ListModelMixin from rest_framework.filters import SearchFilter from . import models, serializers # 搜索课程接口 class SearchCourseViewSet(GenericViewSet, ListModelMixin): queryset = models.Course.objects.filter(is_delete=False, is_show=True).all() serializer_class = serializers.CourseSerializer pagination_class = pagination.PageNumberPagination filter_backends = [SearchFilter] search_fields = ['name']