Can I customize swagger description by comment viewset? I use drf-yasg.
My code:
class MetricViewSet(viewsets.ReadOnlyModelViewSet):
"""
retrieve:
Получить из справочника данные записи о метрике по *uuid*
list:
Получить из справочника коллекцию записей о метриках
"""
lookup_field = 'uuid'
serializer_class = MetricSerializer
queryset = Metric.objects.all()
permission_classes = (ApiPermission,)
my serializer
class MetricSerializer(serializers.ModelSerializer):
"""
Metric serializer.
"""
uuid = serializers.UUIDField(label='Global UUID', read_only=True)
class Meta: # pylint: disable=too-few-public-methods
"""
Настройки сериализатора
"""
model = Metric
fields = ('uuid', 'creation_date', 'modify_date',
'name', 'description', 'enabled', 'comment')
I want add descrription here:
How can I do it?
The following allows you to achieve part of what you asked. That is changing the descriptions of ViewSet's methods, parameters, and responses by using swagger_auto_schema in combination with Django's method_decorator.
Check both links for more info.
from rest_framework import viewsets
from drf_yasg import openapi
from drf_yasg.utils import swagger_auto_schema
@method_decorator(name='retrieve', decorator=swagger_auto_schema(
operation_description="Method description",
operation_id="Method ID",
operation_summary="Method summary",
manual_parameters=[
openapi.Parameter('serial', in_=openapi.IN_PATH, type=openapi.TYPE_STRING, description='Parameter description')
],
responses={'200': 'Response description'}
))
class MetricViewSet(viewsets.ReadOnlyModelViewSet):
"""
retrieve:
Получить из справочника данные записи о метрике по *uuid*
list:
Получить из справочника коллекцию записей о метриках
"""
lookup_field = 'uuid'
serializer_class = MetricSerializer
queryset = Metric.objects.all()
permission_classes = (ApiPermission,)
I found a bug where operation_summary
and deprecated
options passed to swagger_auto_schema
are actually ignored, so I submitted this PR to fix that.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With