-
Notifications
You must be signed in to change notification settings - Fork 438
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Question: pagination on APIView(Post) #575
Comments
I also need answer for this problem, in the documents mentioned that |
I have the same problem - in my case i have custom serializer in list-action in my ModelViewSet, that is different from the
But in generated swagger docs i see just a list of data from SomeCustomSerialaizer, not wrapped in It seems when i specify |
I have sort of fix for that situation. But i have not tested that solution and can say what ripple it can produce. You can crea your own subclass of
|
My solution for this. Only for from drf_yasg import openapi
from drf_yasg.utils import guess_response_status
from drf_yasg.inspectors import SwaggerAutoSchema
class CustomSwaggerAutoSchema(SwaggerAutoSchema):
def get_responses(self):
response_serializers = self.get_response_serializers()
response_schemas = self.get_response_schemas(response_serializers)
paginator = self.overrides.get('paginator', None)
if paginator and self.has_list_response():
method = self.method.lower()
default_response_status = str(guess_response_status(method))
if default_response_status in response_schemas:
response_schemas[default_response_status] = openapi.Response(
description=response_schemas[default_response_status].description,
schema=self.get_paginated_response(
response_schemas[default_response_status].schema
)
)
return openapi.Responses(responses=response_schemas)
def get_paginated_response(self, response_schema):
return self.probe_inspectors(self.paginator_inspectors, 'get_paginated_response',
self._get_paginator(), response_schema=response_schema)
def get_pagination_parameters(self):
if not self.should_page():
return []
return self.probe_inspectors(self.paginator_inspectors, 'get_paginator_parameters',
self._get_paginator()) or []
def should_page(self):
return self._get_paginator() and self.has_list_response()
def _get_paginator(self):
return self.overrides.get('paginator') or getattr(self.view, 'paginator', None) In settings: SWAGGER_SETTINGS = {
...
'DEFAULT_AUTO_SCHEMA_CLASS': 'website_apps.api.utils.swagger.CustomSwaggerAutoSchema',
} In view code: class EntityListView(APIView):
class ListPagination(pagination.CursorPagination):
page_size = 20
@swagger_auto_schema(
operation_description=_('Returns entity list with pagination'),
query_serializer=EntityListQueryFilter(),
responses={
200: openapi.Response(
description=_('Paginated entity list'),
schema=EntityOutputSerializer(many=True)
)
},
paginator=ListPagination()
)
def get(self, request):
serializer = serializers.EntityListQueryFilter(data=request.query_params)
serializer.is_valid(raise_exception=True)
entities = get_entities(**serializer.validated_data)
paginator = self.ListPagination()
entities = paginator.paginate_queryset(entities, request)
serializer = EntityOutputSerializer(entities, many=True)
return paginator.get_paginated_response(serializer.data) |
Hi,
Is it possible to do pagination on a post in an APIView? I don't seem to understand how that could be done. I've got the following:
But the swagger page doesn't show any pagination in the responses model. What am I understanding wrong?
The text was updated successfully, but these errors were encountered: