How to automatically generate django rest framework documentation with parameters?

908 Views Asked by At

I use redoc + drf_yasg to generate the documentation of my api made with django-rest-framwork. It works well as soon as I add an entry point it appears well in the documentation.

However, I don't understand how to add in the documentation the search parameters that can be given during the query.

For example, in the list function, I can pass 2 optional parameters, an id and name:

class TechnicalDataViewSet(viewsets.ViewSet):
    """
    A simple ViewSet for listing or retrieving machine.
    """
    permission_classes = [permissions.IsAuthenticated]
    def list(self, request):
        id_machine = self.request.query_params.get('id_machine')
        name = self.request.query_params.get('name')
        queryset = TechnicalData.objects.all()
        if id_machine:
            queryset = queryset.filter(machine__id=id_machine)
        if name:
            queryset = queryset.filter(name=name)
        serializer = TechnicalDataSerializer(queryset, many=True)
        return Response(serializer.data)

    def retrieve(self, request, pk=None):
        queryset = TechnicalData.objects.all()
        technical_data = get_object_or_404(queryset, pk=pk)
        serializer = TechnicalDataSerializer(technical_data)
        return Response(serializer.data)

screenshot

I don't see any mention of the parameters in the documentation. How can I add them?

I don't know if this helps but here is how I implemented redoc:

schema_view = get_schema_view( 
   openapi.Info( 
      title="test API", 
      default_version='v1', 
      description=f"""
        # Public API
        
        **Reserved to authenticated users**
        
        Token Lifetime :
        
        * ACCESS_TOKEN_LIFETIME : {duration(settings.SIMPLE_JWT.get('ACCESS_TOKEN_LIFETIME'))}
        * REFRESH_TOKEN_LIFETIME : {duration(settings.SIMPLE_JWT.get('REFRESH_TOKEN_LIFETIME'))}
        
        ### Links
        [Get access token](************)
        
        ### Format
        Use `?format=` option in url:
        * `?format=api` (default)
        * `?format=json`
      """, 
      terms_of_service="", 
      contact=openapi.Contact(email="***********"), 
   ), 
   public=True, 
   permission_classes=(permissions.AllowAny,), 
) 

urlpatterns = [
    path('docs/', schema_view.with_ui('redoc', cache_timeout=0), name='schema-redoc'), 
]
1

There are 1 best solutions below

0
On

You have to add your parameters to your view if you want to give info about them. I use drf_yasg with swagger but maybe this might help. Here's an example.

from drf_yasg import openapi
from drf_yasg.utils import swagger_auto_schema


class TechnicalDataViewSet(viewsets.ViewSet):
    """
    A simple ViewSet for listing or retrieving machine.
    """
    permission_classes = [permissions.IsAuthenticated]

    id_machine = openapi.Parameter('id_machine', openapi.IN_QUERY, type=openapi.TYPE_INTEGER, required=False)
    name = openapi.Parameter('account', openapi.IN_QUERY, type=openapi.TYPE_STRING, required=False)

    @swagger_auto_schema(manual_parameters=[id_machine, name])
    def list(self, request):
        id_machine = self.request.query_params.get('id_machine')
        name = self.request.query_params.get('name')
        queryset = TechnicalData.objects.all()
        if id_machine:
            queryset = queryset.filter(machine__id=id_machine)
        if name:
            queryset = queryset.filter(name=name)
        serializer = TechnicalDataSerializer(queryset, many=True)
        return Response(serializer.data)

    @swagger_auto_schema(manual_parameters=[id_machine, name])
    def retrieve(self, request, pk=None):
        queryset = TechnicalData.objects.all()
        technical_data = get_object_or_404(queryset, pk=pk)
        serializer = TechnicalDataSerializer(technical_data)
        return Response(serializer.data)