前言
rest framework中提供了三种分页模式:
from rest_framework.pagination import PageNumberPagination, LimitOffsetPagination, CursorPagination
在我的项目中我使用的是PageNumberPagination
在全局配置文件,在用到的使用才会引入,有点像配置文件
REST_FRAMEWORK = { 'rest_framework.pagination.PageNumberPagination', 'PAGE_SIZE':20, }
去实现
class text_list(APIView): """ get: 获取列表页 """ def get(self, request): coin_model=models.objects.all().order_by("-id") //使用继承的方式实现分页对象 pg = MyPageNumberPagination() objs = pg.paginate_queryset(coin_model, request,self) ser = serializers.Serializer(objs, many=True) data=pg.get_response_data(ser.data, 'list') return response_util.ok(data=data)
class MyPageNumberPagination(PageNumberPagination): page_size = 6 max_page_size = 100 page_size_query_param = 'page_size' page_query_param = 'page_index' def get_response_data(self, data, data_key='results', **kwargs): data = { 'page_total': self.page.paginator.count, 'page_index': self.page.number, 'page_size': len(data), data_key: data } data.update(**kwargs) return data
from rest_framework import serializers from . import models class Serializer(serializers.ModelSerializer): class Meta: model = models.TokenTpl fields = [ //序列化的字段,也就是要输出的字段 'id','name' ]
参考链接:
https://blog.csdn.net/fenglepeng/article/details/104699141
本文来自博客园,作者:topass123,转载请注明原文链接:https://www.cnblogs.com/topass123/p/16307609.html