six_001
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
from rest_framework import status
|
||||
from rest_framework.decorators import action
|
||||
from rest_framework.pagination import PageNumberPagination
|
||||
from rest_framework.request import Request
|
||||
from rest_framework.response import Response
|
||||
from rest_framework.routers import DefaultRouter
|
||||
from rest_framework.viewsets import ViewSet
|
||||
|
||||
from sixGuoDjango import BaseResponse, ResData
|
||||
|
||||
|
||||
# 创建一个自定义分页器
|
||||
class CustomPagination(PageNumberPagination):
|
||||
page_size = 10 # 默认每页显示10条
|
||||
page_size_query_param = 'page_size' # 允许客户端通过参数指定每页数量
|
||||
max_page_size = 100 # 最大每页数量限制
|
||||
page_query_param = 'page' # 页码参数名
|
||||
|
||||
|
||||
class 分页接口ViewSets(ViewSet):
|
||||
|
||||
@action(detail=False, methods=["get", "post", "put", "delete"], url_path="xio")
|
||||
def 分页器接口(self, request: Request):
|
||||
""""""
|
||||
# page_all = xiaozizi.objects.all( )
|
||||
|
||||
# 创建分页器实例
|
||||
paginator = CustomPagination()
|
||||
|
||||
# 对查询集进行分页
|
||||
# paginated_data = paginator.paginate_queryset( queryset=page_all, request=request )
|
||||
|
||||
# 序列化数据
|
||||
# page_list_data = [ ]
|
||||
# print( paginated_data )
|
||||
# for item in paginated_data:
|
||||
# print( type( item ) )
|
||||
# print( model_to_dict( instance=item, fields=None ) )
|
||||
# page_list_data.append( model_to_dict( instance=item, fields=None ) )
|
||||
# listData = orjson.dumps(paginated_data, option=orjson.OPT_SORT_KEYS, ).decode(encoding="utf-8")
|
||||
httpResData = BaseResponse(
|
||||
resData=ResData(listData=[], dictData={"demo": "xiaozizi"}), ).model_dump()
|
||||
return Response(data=httpResData, status=status.HTTP_200_OK)
|
||||
|
||||
|
||||
分页接口ViewSetsRouter = DefaultRouter()
|
||||
分页接口ViewSetsRouter.register(prefix="", viewset=分页接口ViewSets, basename="")
|
||||
Reference in New Issue
Block a user