2025-04-29 14:56:10 +08:00
|
|
|
|
from rest_framework.pagination import PageNumberPagination
|
|
|
|
|
from rest_framework.response import Response
|
|
|
|
|
|
|
|
|
|
class CustomPagination(PageNumberPagination):
|
|
|
|
|
"""自定义分页器,返回格式为 {code, message, data}"""
|
|
|
|
|
page_size = 10
|
|
|
|
|
page_size_query_param = 'page_size'
|
|
|
|
|
max_page_size = 100
|
|
|
|
|
|
|
|
|
|
def get_paginated_response(self, data):
|
2025-05-14 22:49:55 +08:00
|
|
|
|
# 为每个结果添加name字段,从platforms中提取平台名称
|
|
|
|
|
for item in data:
|
|
|
|
|
if 'platforms' in item and len(item['platforms']) > 0:
|
|
|
|
|
# 添加name字段,只使用platform_name
|
|
|
|
|
platform = item['platforms'][0]
|
|
|
|
|
item['name'] = platform.get('platform_name', '')
|
|
|
|
|
|
2025-04-29 14:56:10 +08:00
|
|
|
|
return Response({
|
|
|
|
|
"code": 200,
|
|
|
|
|
"message": "获取数据成功",
|
|
|
|
|
"data": {
|
|
|
|
|
"count": self.page.paginator.count,
|
|
|
|
|
"next": self.get_next_link(),
|
|
|
|
|
"previous": self.get_previous_link(),
|
|
|
|
|
"results": data,
|
|
|
|
|
"page": self.page.number,
|
|
|
|
|
"pages": self.page.paginator.num_pages,
|
|
|
|
|
"page_size": self.page_size
|
|
|
|
|
}
|
|
|
|
|
})
|