1、前端接口分页,引用CustomPagination ,分页参数page页数和limit条数,方法如下:
from utils.pagination import CustomPagination
#按照手机号搜索内部用户并实现分页
class SearchUserView(APIView):
"""
按照手机号搜索内部用户并实现分页
get:
【参数说明】
mobile : 搜索字段
"""
authentication_classes = [JWTAuthentication]
permission_classes = [IsAuthenticated]
def get(self, request):
mobile = int(get_parameter_dic(request)['mobile'])
if mobile is None or mobile == "":
return ErrorResponse(msg='please input your search content')
queryset = Users.objects.filter(mobile__contains=mobile).order_by('-create_datetime')
# # 1. 实例化分页器对象
page_obj = CustomPagination()
# # 2. 使用自己配置的分页器调用分页方法进行分页
page_data = page_obj.paginate_queryset(queryset, request)
data = []
if queryset:
for m in page_data:
data.append({
'id':m.id,
'nickname':m.nickname,
'avatar':m.avatar,
'mobile':m.mobile
})
return page_obj.get_paginated_response(data=data)