django drf Filter
生活随笔
收集整理的這篇文章主要介紹了
django drf Filter
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1.定義get_queryset()方法
from django.shortcuts import renderfrom 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 DjangoFilterBackendclass GoodsList(mixins.ListModelMixin, viewsets.GenericViewSet):class GoodsPagination(PageNumberPagination):page_size = 2page_size_query_param = 'pageSize'page_query_param = 'p'max_page_size = 100# queryset = Goods.objects.all() # 不能切片后再過濾,例如:Goods.objects.all()[:10]serializer_class = GoodsSerializerpagination_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 renderfrom 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 DjangoFilterBackendclass GoodsList(mixins.ListModelMixin, viewsets.GenericViewSet):class GoodsPagination(PageNumberPagination):page_size = 2page_size_query_param = 'pageSize'page_query_param = 'p'max_page_size = 100queryset = Goods.objects.all() # 不能切片后再過濾,例如:Goods.objects.all()[:10]serializer_class = GoodsSerializerpagination_class = GoodsPaginationfilter_backends = (DjangoFilterBackend,)filter_fields = ('name',) #逗號必加,缺點無法模糊查詢test
3.自定義filter
from django.shortcuts import renderfrom 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 DjangoFilterBackendimport 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 = Goodsfields = ['min_price','max_price']class GoodsList(mixins.ListModelMixin, viewsets.GenericViewSet):class GoodsPagination(PageNumberPagination):page_size = 2page_size_query_param = 'pageSize'page_query_param = 'p'max_page_size = 100queryset = Goods.objects.all() # 不能切片后再過濾,例如:Goods.objects.all()[:10]serializer_class = GoodsSerializerpagination_class = GoodsPaginationfilter_backends = (DjangoFilterBackend,)# filter_fields = ('name',) #逗號必加,缺點無法模糊查詢filterset_class = GoodsFiltertest
?
轉載于:https://www.cnblogs.com/chenyishi/p/10656604.html
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的django drf Filter的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 销售额同比增长怎么算
- 下一篇: 什么是按揭贷款