I am struggling to properly generate the schema for my custom pagination in django rest framework. I am using drf-spectacular for the schema generation. My custom pagination includes a total-pages field which does not come with djangos PageNumberPagination. The response is correctly serialized and returned and includes the total-pages, but the schema in my swagger docs does not include the field. Here is my code:
pagination.py
from rest_framework import pagination
from rest_framework.response import Response
class CustomPagination(pagination.PageNumberPagination):
page_size = 10
page_size_query_param = 'page_size'
max_page_size = 100
page_query_param = 'p'
def get_paginated_response(self, data):
return Response({
'page_size': self.page_size,
'total_objects': self.page.paginator.count,
'total_pages': self.page.paginator.num_pages,
'current_page_number': self.page.number,
'next': self.get_next_link(),
'previous': self.get_previous_link(),
'results': data,
})
Here is my view:
views.py
@extend_schema_view(
get=extend_schema(
parameters=[OpenApiParameter("q", OpenApiTypes.STR, OpenApiParameter.QUERY),],
request=TestStandardSearchSerializer,
responses=TestStandardSerializer
)
)
class TestStandardSearchView(ListAPIView):
serializer_class = TestStandardSearchSerializer
queryset = TestStandard.objects.all()
pagination_class = CustomPagination
def get(self, request, *args, **kwargs):
query = self.request.query_params.get('q')
queryset = SearchQuerySet().all().filter(content=query).order_by('acronym')
page = self.paginate_queryset(queryset)
serializer = self.get_serializer(page, many=True)
return self.get_paginated_response(serializer.data)
def get_serializer_class(self):
if self.request.method == 'GET':
return TestStandardSearchSerializer
The response schema from my swagger doc is the following:
PaginatedTestStandardList
{
count integer example: 123
next string($uri) nullable: true example: http://api.example.org/accounts/?p=4
previous string($uri) nullable: true example: http://api.example.org/accounts/?p=2
results [TestStandard{...}]
}
The standard django pagination is correctly ínserted in the schema, but not my custom pagination response. What I expected/wanted is to have my customized pagination response correctly integrated with the total-pages field on the same level as 'count', 'next' and 'previous'.
What I tried... I have a working solution with drf_yasg using the PaginatorInspector providing a custom schema. But this is not available in drf-spectacular. I also used inline_serializer with a custom response in @extend_schema_view such as:
responses={
200: inline_serializer(
name='PaginatedTestStandardSearchResponse',
fields={
'total-pages': serializers.IntegerField(),
'results': TestStandardSearchSerializer()
},
This resulted in a schema where total-pages is nested within results. I am using:
drf-spectacular 0.21.2
Django 3.2.12
django-rest-swagger 2.2.0
djangorestframework 3.12.4
Any help is appreciated. I just recently started with django rfw and openapi schema generation. Sorry if I had missed something obvious here.