如何从django rest框架的基于函数的视图中分页响应?

时间:2022-07-14 19:37:41

I have written a code snippet like below, i need to achieve pagination in this, kindly let me know how is it possible. Also due to some reasons i want to use only function based views.

我已经编写了如下代码片段,我需要在此实现分页,请让我知道它是如何可能的。也是由于某些原因,我想只使用基于功能的视图。

@api_view(['GET',])

@permission_classes([AllowAny,])

def PersonView(request):

context={'request': request}
person_objects = Person.objects.all()
if len(person_objects)> 0:
    person_data =     PersonSerializer(person_objects,many=True,context=context)
    return Response(person_data.data,status=status.HTTP_200_OK)
else:
    return Response({},status=status.HTTP_200_OK)

2 个解决方案

#1


13  

http://www.django-rest-framework.org/api-guide/pagination/

http://www.django-rest-framework.org/api-guide/pagination/

from rest_framework.pagination import PageNumberPagination

@api_view(['GET',])

@permission_classes([AllowAny,])

def PersonView(request):

    context={'request': request}
    paginator = PageNumberPagination()
    paginator.page_size = 10
    person_objects = Person.objects.all()
    result_page = paginator.paginate_queryset(person_objects, request)
    serializer = PersonSerializer(result_page, many=True)
    return paginator.get_paginated_response(serializer.data)

#2


1  

You can also define custom pagination class by overriding PageNumberPagination

您还可以通过覆盖PageNumberPagination来定义自定义分页类

pagination.py

pagination.py

from rest_framework import pagination
class StandardResultsSetPagination(pagination.PageNumberPagination):
    page_size = 10
    page_query_param = 'page'
    page_size_query_param = 'per_page'
    max_page_size = 1000

it will help to define

它将有助于定义

page_size, page query custom parameters and max_page_size

page_size,页面查询自定义参数和max_page_size

views.py

views.py

from rest_api.pagination import StandardResultsSetPagination

@api_view(['GET',])
@permission_classes([AllowAny,])
def PersonView(request):
    person_objects = Person.objects.all()
    if len(person_objects)> 0:
        paginator = StandardResultsSetPagination()
        result_page = paginator.paginate_queryset(person_objects, request)
        serializer = PersonSerializer(result_page, many=True)
        return paginator.get_paginated_response(serializer.data)
    else:
        return Response({},status=status.HTTP_200_OK)

Eg: Request

例如:请求

GET https://api.example.org/persons/?page=1&per_page=10

Response

响应

HTTP 200 OK
{
    "count": 1023
    "next": "https://api.example.org/persons/?page=2&per_page=10",
    "previous": null,
    "results": [
       …
    ]
}

#1


13  

http://www.django-rest-framework.org/api-guide/pagination/

http://www.django-rest-framework.org/api-guide/pagination/

from rest_framework.pagination import PageNumberPagination

@api_view(['GET',])

@permission_classes([AllowAny,])

def PersonView(request):

    context={'request': request}
    paginator = PageNumberPagination()
    paginator.page_size = 10
    person_objects = Person.objects.all()
    result_page = paginator.paginate_queryset(person_objects, request)
    serializer = PersonSerializer(result_page, many=True)
    return paginator.get_paginated_response(serializer.data)

#2


1  

You can also define custom pagination class by overriding PageNumberPagination

您还可以通过覆盖PageNumberPagination来定义自定义分页类

pagination.py

pagination.py

from rest_framework import pagination
class StandardResultsSetPagination(pagination.PageNumberPagination):
    page_size = 10
    page_query_param = 'page'
    page_size_query_param = 'per_page'
    max_page_size = 1000

it will help to define

它将有助于定义

page_size, page query custom parameters and max_page_size

page_size,页面查询自定义参数和max_page_size

views.py

views.py

from rest_api.pagination import StandardResultsSetPagination

@api_view(['GET',])
@permission_classes([AllowAny,])
def PersonView(request):
    person_objects = Person.objects.all()
    if len(person_objects)> 0:
        paginator = StandardResultsSetPagination()
        result_page = paginator.paginate_queryset(person_objects, request)
        serializer = PersonSerializer(result_page, many=True)
        return paginator.get_paginated_response(serializer.data)
    else:
        return Response({},status=status.HTTP_200_OK)

Eg: Request

例如:请求

GET https://api.example.org/persons/?page=1&per_page=10

Response

响应

HTTP 200 OK
{
    "count": 1023
    "next": "https://api.example.org/persons/?page=2&per_page=10",
    "previous": null,
    "results": [
       …
    ]
}