1.在Lib\site-packages\rest_framework\settings.py中查看:前端
2.簡單分頁在項目setting中配置:(全部get請求返回數據每頁5條)django
#每頁五條數據 REST_FRAMEWORK={ 'DEFAULT_PAGINATION_CLASS':'rest_framework.pagination.PageNumberPagination', 'PAGE_SIZE':5 }
from rest_framework.pagination import PageNumberPagination #深度定製分頁 class GoodsPagination(PageNumberPagination): page_size = 12 page_size_query_param = 'page_size' #名稱 page_query_param = 'page' #單頁數量最多 max_page_size = 100 class GoodsListViewSet(CacheResponseMixin,mixins.ListModelMixin,mixins.RetrieveModelMixin,viewsets.GenericViewSet): """ 商品詳情頁,分頁,搜索,過濾,排序 """ #配置ip限制訪問次數 throttle_classes = (UserRateThrottle,AnonRateThrottle) queryset = Goods.objects.all() serializer_class = GoodsSerializer #分頁,調用分頁定製類 pagination_class = GoodsPagination
直接經過get_queryset簡單過濾:api
lass GoodsListViewSet(mixins.ListModelMixin,viewsets.GenericViewSet): """ 商品詳情頁 """ serializer_class = GoodsSerializer #分頁 pagination_class = GoodsPagination def get_queryset(self):
#接受前端傳遞的數據
queryset = Goods.objects.all()
min_price = self.request.query_params.get('price_min',0)
if min_price:
queryset=queryset.filter(shop_price__gt=int(min_price)) return queryset
經過filter進行過濾:app
安裝:pip install django-filteride
註冊app:把django-filters註冊到setting中的install-app中ui
在view中導入from django_filters.rest_framework import DjangoFilterBackend,並在viewset中寫入filter_backends=(DjangoFilterBackend,)spa
from django_filters.rest_framework import DjangoFilterBackend class GoodsListViewSet(CacheResponseMixin,mixins.ListModelMixin,mixins.RetrieveModelMixin,viewsets.GenericViewSet): """ 商品詳情頁,分頁,搜索,過濾,排序 """ #配置ip限制訪問次數 throttle_classes = (UserRateThrottle,AnonRateThrottle) queryset = Goods.objects.all() serializer_class = GoodsSerializer #分頁 pagination_class = GoodsPagination #配置認證類,防止公開網頁(未登陸可查看)不能訪問 # authentication_classes = (TokenAuthentication,) filter_backends=(DjangoFilterBackend,filters.SearchFilter,filters.OrderingFilter) #字段過濾(DjangoFilterBackend) # filter_fields = ('name', 'shop_price')
#定製的filter
filter_class=GoodsFilter #搜索過濾(rest_framework.filters.SearchFilter) search_fields = ('name','goods_brief','goods_desc') #排序過濾(rest_frameworkfilters.OrderingFilter) ordering_fields = ('sold_num', 'shop_price') def retrieve(self, request, *args, **kwargs): instance = self.get_object() instance.click_num+=1 instance.save() serializer = self.get_serializer(instance) return Response(serializer.data)
定製的篩選器:3d
1 import django_filters 2 from django.db.models import Q 3 4 from .models import Goods 5 6 7 class GoodsFilter(django_filters.rest_framework.FilterSet): 8 #help_text--docs:description 9 pricemin=django_filters.NumberFilter(field_name='shop_price',lookup_expr='gte',help_text='最低價格') 10 pricemax=django_filters.NumberFilter(field_name='shop_price',lookup_expr='lte') 11 # name=django_filters.CharFilter(field_name='name',lookup_expr='icontains') 12 top_category=django_filters.NumberFilter(method='top_category_filter') 13 #定製的過濾方法 14 def top_category_filter(self,queryset,name,value): 15 return queryset.filter(Q(category_id=value)|Q(category__parent_category_id=value)|Q(category__parent_category__parent_category_id=value)) 16 17 18 class Meta: 19 model=Goods 20 fields=['pricemin','pricemax','top_category','is_hot','is_new']
搜索:rest
from rest_framework import filters class GoodsListViewSet(CacheResponseMixin,mixins.ListModelMixin,mixins.RetrieveModelMixin,viewsets.GenericViewSet): """ 商品詳情頁,分頁,搜索,過濾,排序 """ #配置ip限制訪問次數 throttle_classes = (UserRateThrottle,AnonRateThrottle) queryset = Goods.objects.all() serializer_class = GoodsSerializer #分頁 pagination_class = GoodsPagination #配置認證類,防止公開網頁(未登陸可查看)不能訪問 # authentication_classes = (TokenAuthentication,) filter_backends=(DjangoFilterBackend,filters.SearchFilter,filters.OrderingFilter) #字段過濾(DjangoFilterBackend) # filter_fields = ('name', 'shop_price') filter_class=GoodsFilter #搜索過濾(rest_framework.filters.SearchFilter) search_fields = ('name','goods_brief','goods_desc') #排序過濾(rest_frameworkfilters.OrderingFilter) ordering_fields = ('sold_num', 'shop_price') def retrieve(self, request, *args, **kwargs): instance = self.get_object() instance.click_num+=1 instance.save() serializer = self.get_serializer(instance) return Response(serializer.data)
排序:code
建議您明確指定API應在訂購過濾器中容許哪些字段。您能夠經過ordering_fields
在視圖上設置屬性來執行此操做,以下所示:
class UserListView(generics.ListAPIView): queryset = User.objects.all() serializer_class = UserSerializer filter_backends = (filters.OrderingFilter,) ordering_fields = ('username', 'email')
這有助於防止意外數據泄漏,例如容許用戶針對密碼哈希字段或其餘敏感數據進行排序。
若是未ordering_fields
在視圖上指定屬性,則篩選器類將默認容許用戶篩選serializer_class
屬性指定的序列化程序上的任何可讀字段。
若是您確信視圖使用的查詢集不包含任何敏感數據,則還能夠使用特殊值明確指定視圖應容許對任何模型字段或查詢集合進行排序'__all__'
。
class BookingsListView(generics.ListAPIView): queryset = Booking.objects.all() serializer_class = BookingSerializer filter_backends = (filters.OrderingFilter,) ordering_fields = '__all__'
若是ordering
在視圖上設置了屬性,則將其用做默認排序。
一般,您能夠經過設置order_by
初始查詢集來控制它,可是使用ordering
視圖上的參數容許您以一種方式指定排序,而後能夠將其做爲上下文自動傳遞給呈現的模板。若是列標題用於排序結果,則能夠自動呈現列標題。
class UserListView(generics.ListAPIView): queryset = User.objects.all() serializer_class = UserSerializer filter_backends = (filters.OrderingFilter,) ordering_fields = ('username', 'email') ordering = ('username',)
該ordering
屬性能夠是字符串或字符串的列表/元組。
from rest_framework import filters class GoodsListViewSet(CacheResponseMixin,mixins.ListModelMixin,mixins.RetrieveModelMixin,viewsets.GenericViewSet): """ 商品詳情頁,分頁,搜索,過濾,排序 """ #配置ip限制訪問次數 throttle_classes = (UserRateThrottle,AnonRateThrottle) queryset = Goods.objects.all() serializer_class = GoodsSerializer #分頁 pagination_class = GoodsPagination #配置認證類,防止公開網頁(未登陸可查看)不能訪問 # authentication_classes = (TokenAuthentication,) filter_backends=(DjangoFilterBackend,filters.SearchFilter,filters.OrderingFilter) #字段過濾(DjangoFilterBackend) # filter_fields = ('name', 'shop_price') filter_class=GoodsFilter #搜索過濾(rest_framework.filters.SearchFilter) search_fields = ('name','goods_brief','goods_desc') #排序過濾(rest_frameworkfilters.OrderingFilter) ordering_fields = ('sold_num', 'shop_price') def retrieve(self, request, *args, **kwargs): instance = self.get_object() instance.click_num+=1 instance.save() serializer = self.get_serializer(instance) return Response(serializer.data)