如何解决drf-yasg:在 Swagger 文档中显示 ListAPIView 的自定义分页
我在 Django REST framework 中有以下 ListAPIView 和自定义分页类:
views.py
class pricetrend(generics.ListAPIView):
queryset = Variants.objects.annotate(timestamp=TruncMonth('variantTimestamp')).values('timestamp').annotate(average_price=Avg('price'))
serializer_class = PricetrendSerializer
pagination_class = PricesPagination
class PricesPagination(PageNumberPagination):
page_size = 10
page_size_query_param = 'page_size'
def get_paginated_response(self,data):
prices = [dict(item)['average_price'] for item in data]
try:
total_average_price = sum(prices)/ len(prices)
except Exception as e:
total_average_price = 0
return Response({
'count': self.page.paginator.count,'next': self.get_next_link(),'prevIoUs': self.get_prevIoUs_link(),'total_average_price': round(total_average_price),'results': data,})
目前,我想弄清楚如何在由 drf-yasg 生成的 Swagger API 文档中显示自定义分页类。
我已经从 drf_yasg.inspectors 自定义了 PaginatorInspector,但不知道我需要把它放在哪里才能将它用于上述 ListAPIView。
from drf_yasg.inspectors import PaginatorInspector
from drf_yasg import openapi
class LimitOffsetPaginatorInspectorClass(PaginatorInspector):
def get_paginated_response(self,paginator,response_schema):
"""
:param BasePagination paginator: the paginator
:param openapi.Schema response_schema: the response schema that must be paged.
:rtype: openapi.Schema
"""
return openapi.Schema(
type=openapi.TYPE_OBJECT,properties=OrderedDict((
('count',openapi.Schema(type=openapi.TYPE_INTEGER) if has_count else None),('next',openapi.Schema(type=openapi.TYPE_STRING,format=openapi.FORMAT_URI,x_nullable=True)),('prevIoUs',('total_average_price',openapi.Schema(type=openapi.TYPE_INTEGER)),('results',response_schema),)),required=['results']
)
由于我使用具有 settings.py 中指定的默认分页类的其他 ListAPIView,自定义分页类应仅用于 ListAPIView“pricetrend”。
解决方法
解决方法如下:
- 创建自定义分页器检查器
分页器检查员
class LimitOffsetPaginatorInspectorClass(PaginatorInspector):
def get_paginated_response(self,paginator,response_schema):
"""
:param BasePagination paginator: the paginator
:param openapi.Schema response_schema: the response schema that must be paged.
:rtype: openapi.Schema
"""
return openapi.Schema(
type=openapi.TYPE_OBJECT,properties=OrderedDict((
('count',openapi.Schema(type=openapi.TYPE_INTEGER)),('next',openapi.Schema(type=openapi.TYPE_STRING,format=openapi.FORMAT_URI,x_nullable=True)),('previous',('total_average_price',('results',response_schema),)),required=['results']
)
- 覆盖 ListAPIView 以便能够使用@swagger_auto_schema 指定自定义分页器检查器
views.py
class pricetrend(generics.ListAPIView):
queryset = Variants.objects.annotate(timestamp=TruncMonth('variantTimestamp')).values('timestamp').annotate(average_price=Avg('price'))
serializer_class = PricetrendSerializer
pagination_class = PricesPagination
@swagger_auto_schema(pagination_class=PricesPagination,paginator_inspectors=[LimitOffsetPaginatorInspectorClass])
def get(self,request,*args,**kwargs):
queryset = self.filter_queryset(self.get_queryset())
page = self.paginate_queryset(queryset)
serializer = PricetrendSerializer(page,many=True)
return self.get_paginated_response(serializer.data)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。