ChantOfSpirit
ChantOfSpirit

Reputation: 156

Django REST: No pagination block in JSON response (PageNumberPagination)

For my API I'm using api_view decorator. Problem with pagination (JSON response). I got response without "pagination block":

[
    {
        "id": 18,
        "name": "Monitor Acer Test",
        "price": "2212.00",
        "stock": 21,
        "image": "/media/9hq.webp",
        "available": true
    },
    {
        "id": 17,
        "name": "Monitor LG Test",
        "price": "2512.00",
        "stock": 10,
        "image": "/media/811AFxM28YL._SX425_.jpg",
        "available": true
    }
]

I also tried to override default PageNumberPagination, it works, but still without "pagination block"

My api view (look at GET example > else):

@csrf_exempt
@api_view(['GET', 'POST',])
@permission_classes([AllowAny, ])
def product(request):
    item = request.data
    if request.method == 'POST':
        serializer = ProductSerializer(data=item)
        serializer.is_valid(raise_exception=True)
        serializer.save()
        return Response({"message": "Product created!", "data": request.data}, status=status.HTTP_201_CREATED)
    else:
        all_obj = Product.objects.filter(available=True)
        if len(all_obj) > 0:
            paginator = PageNumberPagination()
            result_page = paginator.paginate_queryset(all_obj, request)
            serializer = ProductSerializer(result_page, many=True)
            return Response(serializer.data, status=status.HTTP_200_OK)
        else:
            return Response({"message": "There is no created items!"}, status=status.HTTP_200_OK)

My settings.py:

REST_FRAMEWORK = {
  'DEFAULT_AUTHENTICATION_CLASSES': (
    'rest_framework_jwt.authentication.JSONWebTokenAuthentication',),

  'DEFAULT_PERMISSION_CLASSES': ['rest_framework.permissions.AllowAny',],
  'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
  'PAGE_SIZE': 2
}

Expected result:

{
    "count": 1023
    "next": "https://api.example.org/accounts/?page=5",
    "previous": "https://api.example.org/accounts/?page=3",
    "results": [
       …
    ]
}

Upvotes: 1

Views: 674

Answers (1)

JPG
JPG

Reputation: 88429

You need to call get_paginated_response() method,

@csrf_exempt
@api_view(['GET', 'POST', ])
@permission_classes([AllowAny, ])
def product(request):
    item = request.data
    if request.method == 'POST':
        serializer = ProductSerializer(data=item)
        serializer.is_valid(raise_exception=True)
        serializer.save()
        return Response({"message": "Product created!", "data": request.data}, status=status.HTTP_201_CREATED)
    else:
        all_obj = Product.objects.filter(available=True)

        paginator = PageNumberPagination()
        result_page = paginator.paginate_queryset(all_obj, request)
        if result_page is not None:
            serializer = ProductSerializer(result_page, many=True)
            return paginator.get_paginated_response(serializer.data)
        else:
            serializer = ProductSerializer(all_obj, many=True)
        return Response(serializer.data, status=status.HTTP_200_OK)

NOTE do not call len(queryset), it will cause N number of DB connections. Use count() method instead

Upvotes: 2

Related Questions