1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
|
from rest_framework.viewsets import GenericViewSet from rest_framework.mixins import ListModelMixin, RetrieveModelMixin from . import models, serializers
class CourseViewSet(GenericViewSet, ListModelMixin, RetrieveModelMixin): queryset = models.Course.objects.filter(is_delete=False, is_show=True).all() serializer_class = serializers.CourseSerializer
# 分页组件 pagination_class = pagination.PageNumberPagination # 过滤组件:实际开发,有多个过滤条件时,要把优先级高的放在前面
filter_backends = [SearchFilter, OrderingFilter, DjangoFilterBackend] # 参与搜索的字段 # search_fields = ['name', 'id', 'brief']
# 允许排序的字段 ordering_fields = ['id', 'price']
# 过滤类:分类过滤、区间过滤 filter_class = CourseFilterSet
|