在 Django rest framework pagination 添加新值
Add new values at Django rest framework pagination
我想在分页return处添加'isSuccess'。
例如,
{
"count": 1234,
"next": "http://mydomain/?page=2",
"previous": null,
"isSuccess" : 'Success' # <---- this one
"results": [
{
'id':123,
'name':'abc'
},
{
'id':234,
'name':'efg'
},
...
]
}
我找到了 ,但没用。 如何在 Django 分页中添加新值 return?
这是我的尝试:
class Testing(generics.GenericAPIView):
queryset = Problem.objects.all()
serializer_class = userSerializer
pagination_class = userPagination
def get(self, request):
queryset = self.get_queryset()
page = self.request.query_params.get('page')
if page is not None:
paginate_queryset = self.paginate_queryset(queryset)
serializer = self.serializer_class(paginate_queryset, many=True)
tmp = serializer.data
tmp['isSuccess'] = 'Success'
return self.get_paginated_response(tmp)
试一试,不要将 isSuccess
添加到 serializer.data,而是将其添加到 get_paginated_response().data
def get(self, request):
queryset = self.get_queryset()
page = self.request.query_params.get('page')
if page is not None:
paginate_queryset = self.paginate_queryset(queryset)
serializer = self.serializer_class(paginate_queryset, many=True)
tmp = serializer.data
# tmp['isSuccess'] = 'Success'
response = self.get_paginated_response(tmp)
response.data['isSuccess'] = "Success"
return Response(data=response.data, status=status.HTTP_200_OK)
return Response(data="No Page parameter found", status=status.HTTP_200_OK)
响应类似于
{
"count": 1234,
"next": null,
"previous": null,
"results": [
{
'id':123,
'name':'abc'
},
...
],
"isSuccess" : 'Success'
}
您可以尝试覆盖 PageNumberPagination
中的 get_paginated_response
方法
示例:
from rest_framework.pagination import PageNumberPagination
from collections import OrderedDict
class CustomPagination(PageNumberPagination):
def get_paginated_response(self, data):
return Response(OrderedDict([
('count', self.page.paginator.count),
('next', self.get_next_link()),
('previous', self.get_previous_link()),
('results', data),
('isSuccess', "Success") # extra
]))
我想在分页return处添加'isSuccess'。
例如,
{
"count": 1234,
"next": "http://mydomain/?page=2",
"previous": null,
"isSuccess" : 'Success' # <---- this one
"results": [
{
'id':123,
'name':'abc'
},
{
'id':234,
'name':'efg'
},
...
]
}
我找到了
这是我的尝试:
class Testing(generics.GenericAPIView):
queryset = Problem.objects.all()
serializer_class = userSerializer
pagination_class = userPagination
def get(self, request):
queryset = self.get_queryset()
page = self.request.query_params.get('page')
if page is not None:
paginate_queryset = self.paginate_queryset(queryset)
serializer = self.serializer_class(paginate_queryset, many=True)
tmp = serializer.data
tmp['isSuccess'] = 'Success'
return self.get_paginated_response(tmp)
试一试,不要将 isSuccess
添加到 serializer.data,而是将其添加到 get_paginated_response().data
def get(self, request):
queryset = self.get_queryset()
page = self.request.query_params.get('page')
if page is not None:
paginate_queryset = self.paginate_queryset(queryset)
serializer = self.serializer_class(paginate_queryset, many=True)
tmp = serializer.data
# tmp['isSuccess'] = 'Success'
response = self.get_paginated_response(tmp)
response.data['isSuccess'] = "Success"
return Response(data=response.data, status=status.HTTP_200_OK)
return Response(data="No Page parameter found", status=status.HTTP_200_OK)
响应类似于
{
"count": 1234,
"next": null,
"previous": null,
"results": [
{
'id':123,
'name':'abc'
},
...
],
"isSuccess" : 'Success'
}
您可以尝试覆盖 PageNumberPagination
中的get_paginated_response
方法
示例:
from rest_framework.pagination import PageNumberPagination
from collections import OrderedDict
class CustomPagination(PageNumberPagination):
def get_paginated_response(self, data):
return Response(OrderedDict([
('count', self.page.paginator.count),
('next', self.get_next_link()),
('previous', self.get_previous_link()),
('results', data),
('isSuccess', "Success") # extra
]))