📂drf
2022-10-09 16:56阅读: 52评论: 0推荐: 0

drf 分页 LimitOffsetPagination 简单使用

settings

REST_FRAMEWORK = {
    # 分页,limit大小
    "PAGE_SIZE": 2,
}

views

class BlogView(APIView):

    def get(self, request, *args, **kwargs):
        queryset = models.Blog.objects.order_by('-id').all()
        # 实例化自定义类
        pager = LimitOffsetPagination()
        # 记录最大id
        max_id = request.query_params.get('max_id')
        if max_id:
            # 查询比最大id小的所有数据
            queryset = queryset.filter(id__lt=max_id)
        result = pager.paginate_queryset(queryset, request, self)
        ser = BlogModelSerializer(instance=result, many=True)
        # 把序列化结果给get_paginated_response
        res = pager.get_paginated_response(ser.data)
        res_code.res_success(data=ser.data, msg='查询成功')
        return res

image

本文作者:Sherwin

本文链接:https://www.cnblogs.com/sherwin1995/p/16772790.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   Sherwin_szw  阅读(52)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
💬
评论
📌
收藏
💗
关注
👍
推荐
🚀
回顶
收起
  1. 1 404 not found REOL
404 not found - REOL
00:00 / 00:00
An audio error has occurred.