搜索后台接口
路由:course/urls.py
router.register('search', views.SearchCourseViewSet, 'search') # 搜索课程
视图: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'] # 可以被搜索的字段
选择了IT,必定终身学习