Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add swagger description in comments

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: enter image description here

How can I do it?

like image 499
Vetos Avatar asked Sep 05 '25 03:09

Vetos


1 Answers

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,)

Update

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.

like image 124
Nour Wolf Avatar answered Sep 07 '25 19:09

Nour Wolf