1.定义get_queryset()方法
from django.shortcuts import render from rest_framework.views import APIView from rest_framework.response import Response from rest_framework import status from rest_framework.parsers import JSONParser from rest_framework import mixins, generics from rest_framework.generics import ListAPIView from rest_framework.pagination import PageNumberPagination from goods.models import Goods, GoodsCategory from goods.serializer import GoodsSerializer, CategorySerializer from rest_framework import viewsets from django_filters.rest_framework import DjangoFilterBackend class GoodsList(mixins.ListModelMixin, viewsets.GenericViewSet): class GoodsPagination(PageNumberPagination): page_size = 2 page_size_query_param = 'pageSize' page_query_param = 'p' max_page_size = 100 # queryset = Goods.objects.all() # 不能切片后再过滤,例如:Goods.objects.all()[:10] serializer_class = GoodsSerializer pagination_class = GoodsPagination # 过滤方法1 # 使用此方法,不使用queryset时,须在urls.router中指定base_name参数 def get_queryset(self): queryset = Goods.objects.all() #此时并没有取出数据 price_min = self.request.query_params.get('price_min',0) if price_min: queryset = queryset.filter(shop_price__gt=int(price_min)) return queryset
须在urls中指定base_name
router = DefaultRouter() router.register('goods',GoodsList,base_name='a')
test
2.django-filter
安装步骤相见文档https://django-filter.readthedocs.io/en/master/guide/install.html
https://www.django-rest-framework.org/api-guide/filtering/#djangofilterbackend
View中代码
from django.shortcuts import render from rest_framework.views import APIView from rest_framework.response import Response from rest_framework import status from rest_framework.parsers import JSONParser from rest_framework import mixins, generics from rest_framework.generics import ListAPIView from rest_framework.pagination import PageNumberPagination from goods.models import Goods, GoodsCategory from goods.serializer import GoodsSerializer, CategorySerializer from rest_framework import viewsets from django_filters.rest_framework import DjangoFilterBackend class GoodsList(mixins.ListModelMixin, viewsets.GenericViewSet): class GoodsPagination(PageNumberPagination): page_size = 2 page_size_query_param = 'pageSize' page_query_param = 'p' max_page_size = 100 queryset = Goods.objects.all() # 不能切片后再过滤,例如:Goods.objects.all()[:10] serializer_class = GoodsSerializer pagination_class = GoodsPagination filter_backends = (DjangoFilterBackend,) filter_fields = ('name',) #逗号必加,缺点无法模糊查询
test
3.自定义filter
from django.shortcuts import render from rest_framework.views import APIView from rest_framework.response import Response from rest_framework import status from rest_framework.parsers import JSONParser from rest_framework import mixins, generics from rest_framework.generics import ListAPIView from rest_framework.pagination import PageNumberPagination from goods.models import Goods, GoodsCategory from goods.serializer import GoodsSerializer, CategorySerializer from rest_framework import viewsets from django_filters.rest_framework import DjangoFilterBackend import django_filters class GoodsFilter(django_filters.rest_framework.FilterSet): max_price = django_filters.rest_framework.NumberFilter(field_name='shop_price',lookup_expr='lte') min_price = django_filters.rest_framework.NumberFilter(field_name='shop_price',lookup_expr='gte') class Meta: model = Goods fields = ['min_price','max_price'] class GoodsList(mixins.ListModelMixin, viewsets.GenericViewSet): class GoodsPagination(PageNumberPagination): page_size = 2 page_size_query_param = 'pageSize' page_query_param = 'p' max_page_size = 100 queryset = Goods.objects.all() # 不能切片后再过滤,例如:Goods.objects.all()[:10] serializer_class = GoodsSerializer pagination_class = GoodsPagination filter_backends = (DjangoFilterBackend,) # filter_fields = ('name',) #逗号必加,缺点无法模糊查询 filterset_class = GoodsFilter
test