1.mixins.py中的源码片段
#在mixins.py中的ListModelMixin类中
class ListModelMixin(object):
"""
List a queryset.
"""
def list(self, request, *args, **kwargs):
# 获取数据
queryset = self.filter_queryset(self.get_queryset())
# 将数据交给page做分页
page = self.paginate_queryset(queryset)
if page is not None:
# 分页数据序列化
serializer = self.get_serializer(page, many=True)
# 响应分页数据给前端
return self.get_paginated_response(serializer.data)
serializer = self.get_serializer(queryset, many=True)
return Response(serializer.data)
# 注意: 要是用django-rest-framework中的分页,要在项目的settings.py中进行配置
2.分页的配置
# rest_framework配置
REST_FRAMEWORK = {
# 分頁配置
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
'PAGE_SIZE': 10, # 配置每页显示的数据条数
# 过滤
'DEFAULT_FILTER_BACKENDS': (
'django_filters.rest_framework.DjangoFilterBackend'
),
# 重构renderer
'DEFAULT_RENDERER_CLASSER': (
'utils.renderer.MyJsonRenderer',
)
}