Django drf 分页
Django drf 分页
19.1 全局配置
配置分页类
REST_FRAMEWORK = {
......
# 分页
#'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.LimitOffsetPagination', # 偏移量分页器
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination', # 页码分页器
# 上面两种分页器二选一,个人偏好页码分页器
'PAGE_SIZE': 3, # 每页数据条数
}
视图
# 全局分页的话,视图无需添加新的配置项,只要视图能返回多条数据并且支持drf分页就行
class ExampleView4(ModelViewSet):
queryset = studentsInfo.objects.all()
serializer_class = studentSerializer
filter_backends = [OrderingFilter]
ordering_fields = ["id", "age"]
路由
from . import views
from rest_framework.routers import DefaultRouter
router = DefaultRouter()
router.register("filter", views.ExampleView4, basename="filter")
urlpatterns = [
] + router.urls
访问效果(偏移量分页器)
GET /opt/filter/?ordering=-age
HTTP 200 OK
Allow: GET, POST, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept
{
"count": 8,
"next": "http://127.0.0.1:8000/opt/filter/?limit=3&offset=3&ordering=-age",
"previous": null,
"results": [
{
"id": 17,
"name": "胡歌",
"sex": "男",
"age": 30,
"classroom": "310",
"info": "帅帅帅"
},
{
"id": 3,
"name": "张三",
"sex": "男",
"age": 25,
"classroom": "307",
"info": "这个人很懒,什么也没有留下"
},
{
"id": 4,
"name": "李四",
"sex": "男",
"age": 24,
"classroom": "308",
"info": "这个人很懒,什么也没有留下"
}
]
}
访问效果(页码分页器)
GET /opt/filter/?ordering=-age
HTTP 200 OK
Allow: GET, POST, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept
{
"count": 8,
"next": "http://127.0.0.1:8000/opt/filter/?ordering=-age&page=2",
"previous": null,
"results": [
{
"id": 17,
"name": "胡歌",
"sex": "男",
"age": 30,
"classroom": "310",
"info": "帅帅帅"
},
{
"id": 3,
"name": "张三",
"sex": "男",
"age": 25,
"classroom": "307",
"info": "这个人很懒,什么也没有留下"
},
{
"id": 4,
"name": "李四",
"sex": "男",
"age": 24,
"classroom": "308",
"info": "这个人很懒,什么也没有留下"
}
]
}
19.2 局部配置
配置文件
REST_FRAMEWORK = {
......
'PAGE_SIZE': 3,
}
视图
from rest_framework.filters import OrderingFilter
from rest_framework.pagination import PageNumberPagination
class ExampleView4(ModelViewSet):
queryset = studentsInfo.objects.all()
serializer_class = studentSerializer
filter_backends = [OrderingFilter]
ordering_fields = ["id", "age"]
pagination_class = PageNumberPagination
路由
from . import views
from rest_framework.routers import DefaultRouter
router = DefaultRouter()
router.register("filter", views.ExampleView4, basename="filter")
urlpatterns = [
] + router.urls
访问效果
GET /opt/filter/?ordering=id
HTTP 200 OK
Allow: GET, POST, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept
{
"count": 8,
"next": "http://127.0.0.1:8000/opt/filter/?ordering=id&page=2",
"previous": null,
"results": [
{
"id": 2,
"name": "小红",
"sex": "女",
"age": 18,
"classroom": "301",
"info": "这个人很懒,什么也没有留下"
},
{
"id": 3,
"name": "张三",
"sex": "男",
"age": 25,
"classroom": "307",
"info": "这个人很懒,什么也没有留下"
},
{
"id": 4,
"name": "李四",
"sex": "男",
"age": 24,
"classroom": "308",
"info": "这个人很懒,什么也没有留下"
}
]
}
19.3 自定义分页类
定义分页类
class StudentPageNumberPagination(PageNumberPagination):
page_query_param = "page" # 查询字符串中代表页码的变量名
page_size_query_param = "size" # 查询字符串中代表每一页数据量的变量名
page_size = 2 # 每一页数据量
max_page_size = 8 # 每一页最大数据量
视图
from rest_framework.filters import OrderingFilter
from .paginations import StudentPageNumberPagination
class ExampleView4(ModelViewSet):
queryset = studentsInfo.objects.all()
serializer_class = studentSerializer
filter_backends = [OrderingFilter]
ordering_fields = ["id", "age"]
pagination_class = StudentPageNumberPagination
路由
from . import views
from rest_framework.routers import DefaultRouter
router = DefaultRouter()
router.register("filter", views.ExampleView4, basename="filter")
urlpatterns = [
] + router.urls
访问效果
GET /opt/filter/?ordering=id
HTTP 200 OK
Allow: GET, POST, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept
{
"count": 8,
"next": "http://127.0.0.1:8000/opt/filter/?ordering=id&page=2",
"previous": null,
"results": [
{
"id": 2,
"name": "小红",
"sex": "女",
"age": 18,
"classroom": "301",
"info": "这个人很懒,什么也没有留下"
},
{
"id": 3,
"name": "张三",
"sex": "男",
"age": 25,
"classroom": "307",
"info": "这个人很懒,什么也没有留下"
}
]
}
可以看到,每页的数据量变成了两条,由此可见,分页组件采用了我们自己定义的分页类