DRF分页组件

DRF分页组件三种方式:  

  models.py:  

from django.db import models
 
class UserRole(models.Model):
    role = models.CharField(max_length=12)
models.py:

     urls.py  

from django.conf.urls import url
from api import views
 
urlpatterns = [
    url(r'^(api/v1/roles/', views.UserRoleView.as_view(), name='role'),
]
 
urls.py

  views.py

from api import models
from rest_framework.views import APIView
from rest_framework import serializers
from django.http.response import JsonResponse
from rest_framework.pagination import  PageNumberPagination,LimitOffsetPagination,CursorPagination
 
 
#分页组件(可以直接在全局配置PAGE_SIZE指定每页显示条数使用,也可再自定义类进行继承重写属性)
'''
#(1)第page页,page_size条      PageNumberPagination 
#(2)从offset位置开始,获取limit页    LimitOffsetPagination
#(3)加密cursor游标页码,自带排序!!!,自动上一页下一页   CursorPagination
'''
#序列化器
class UserRoleSerializer1(serializers.ModelSerializer):
    class Meta:
        model = models.UserRole
        fields = "__all__"
 
#(2-1)自定义分页类----继承PageNumberPagination--http://127.0.0.1:8000/api/v1/roles/?page=3&page_size=4
class MyPageNumberPagination(PageNumberPagination):
    #①定义每页默认显示数量--可以在settings.py中定义PAGE_SIZE即可或者在此定义
    page_size = 2
    #②定义页码查询参数,默认page,不指定值默认从1开始
    page_query_param = 'page'
    #③定义每页显示数量查询参数,无默认值,不指定值为默认数量
    page_size_query_param = 'page_size'
    #④定义页面显示最大数量(只针对指定page_size时生效),默认值None
    max_page_size = 1
 
 
# (2-2)自定义分页类----继承LimitOffsetPagination--http://127.0.0.1:8000/api/v1/roles/?offset=9&limit=9
class MyLimitOffsetPagination(LimitOffsetPagination):
    # ①定义每页默认显示数量--可以在settings.py中定义PAGE_SIZE即可或者在此定义
    default_limit = 2
    #②定义查询显示数量查询参数,默认limit,不指定值则为默认数量
    limit_query_param = 'limit'
    # ③定义查询起始位置参数,默认offset,不指定值默认从0开始
    offset_query_param = 'offset'
    # ④定义页面显示最大数量(只针对指定limit时生效),默认值None
    max_limit = 10
 
 
# (2-3)自定义分页类----继承CursorPagination--http://127.0.0.1:8000/api/v1/roles/?size=12
class MyCursorPagination(CursorPagination):
    # ①定义每页默认显示数量--可以在settings.py中定义PAGE_SIZE即可或者在此定义
    page_size = 2
    #②指定页码查询参数,默认cursor,但是不能为其指定值,CursorPagination对页码自行加密了
    cursor_query_param = 'cursor'
    # ③定义每页显示数量查询参数,无默认值,不指定值为默认数量
    page_size_query_param = 'size'
    # ④定义页面显示最大数量(只针对指定size时生效),默认值None
    max_page_size = 10
    #⑤定义排序字段(加负号表示逆序),可以指定单个字段,或者列表,元组
    # ordering = '-id'
    ordering =('-id','role')
 
 
class UserRoleView(APIView):
    filter_backends = ['id']
    def get(self,request,*args,**kwargs):
        #一、获取数据
        roles = models.UserRole.objects.all()
 
        #二、分页:实例化分页对象page
 
        #(1)自带分页组件类(必须在settings.py配置PAGE_SIZE每页显示条数)
        '''
        #(1-1)PageNumberPagination--#http://127.0.0.1:8000/api/v1/roles/?page=4(不指定page默认第一页,取PAGE_SIZE个)
        # page = PageNumberPagination()
 
        #(1-2)LimitOffsetPagination--#http://127.0.0.1:8000/api/v1/roles/?offset=1&limit=1(指定offset开始位置默认0,不指定limit默认取PAGE_SIZE个)
        # page = LimitOffsetPagination()
 
        #(1-3)CursorPagination--自带排序功能(ordering参数指定排序字段,默认ordering = '-created',一般都是自定义使用,自动对页码cursor加密)http://127.0.0.1:8000/api/v1/roles/
        # page = CursorPagination()
        '''
 
        #(2)自定义分页类(继承相关类)
 
        # page =MyPageNumberPagination()#(2-1)
        # page =MyLimitOffsetPagination()#(2-2)
        page =MyCursorPagination()#(2-3)
 
 
        page_roles=page.paginate_queryset(roles,request,view=self)
 
        #三、数据序列化
        ser_roles = UserRoleSerializer1(instance=page_roles,many=True)
        ret =ser_roles.data
 
        #四、返回数据
        # return JsonResponse(ret,safe=False,json_dumps_params={'ensure_ascii':False})#注意序列化结果多条是个列表
        return  page.get_paginated_response(ret)#直接返回总数和前后页链接
 
views.py

  settings.py  

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'api.apps.ApiConfig',
    'rest_framework',
]
 
REST_FRAMEWORK = {
    #分页组件
    'PAGE_SIZE':3
 
}
settings.py
posted @ 2019-09-23 17:24  笑得好美  阅读(333)  评论(0编辑  收藏  举报