1. 程式人生 > >drf分頁配置

drf分頁配置

次數 alt ins elm page listview pack esp 排序

一.drf配置信息:

  1.在Lib\site-packages\rest_framework\settings.py中查看:

技術分享圖片

2.簡單分頁在項目setting中配置:(所有get請求返回數據每頁5條)

#每頁五條數據
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

drf分頁配置