1

I'm trying to integrate the drf-yasg to my Django Rest project. I installed the library via pip and added these code lines to the url.py as below.

schema_view = get_schema_view(
    openapi.Info(
        title="Costifier API",
        default_version='v1',
        description="Costifier API'ye hoşgeldiniz.",
        terms_of_service="https://costifier.sfmyazilim.com",
        contact=openapi.Contact(email="info@sfmyazilim.com"),
    ),
    public=True,
    permission_classes=(permissions.AllowAny,),
)


urlpatterns = [
    path('', index),
    path('admin/', admin.site.urls),
    path('redoc/', schema_view.with_ui('redoc', cache_timeout=0), name='schema-redoc'),  #<-- Here
    path('api/', include('sfmAPI.urls')),
]

One of my views is;

class PredictionView(views.APIView):
    permission_classes = [AllowAny]
    throttle_classes = [AnonymousUserThrottle]

    queryset = Prediction.objects.all()
    serializer_class = PredictionSerializer
    def post(self, request, format=None):
        serializer = PredictionSerializer(data=request.data)
        if serializer.is_valid():
            input_map_dict = json.loads(serializer.validated_data['input_map'])
            username = serializer.validated_data['customer_name']
            prediction_results = SmartRegression.smart_predict(username,
                                                            serializer.validated_data['model_name'],
                                                            input_map_dict,
                                                            isMember(username))
            result = {
                'inputs': serializer.data,
                'error': '0',
                'message': 'Successful',
                'predicted_value': prediction_results[0],
                'confidence': prediction_results[1],
                'feature_importance': prediction_results[2]
            }
            return Response(status=status.HTTP_200_OK, data=result)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

My /redoc page is created. However, it has no content. It just has the APIView names such as below.

enter image description here

How can I fill the documentation?

Ryan
  • 33
  • 1
  • 8
Aslı Kök
  • 616
  • 8
  • 19

1 Answers1

0

The reason is you're using APIView instead of a generic view.

In the Django-Rest-Framework docs for schemas it mentions the below:

Note: The automatic introspection of components, and many operation parameters relies on the relevant attributes and methods of GenericAPIView: get_serializer(), pagination_class, filter_backends, etc. For basic APIView subclasses, default introspection is essentially limited to the URL kwarg path parameters for this reason.

A work around for this if you want to stick to using APIView instead of a generic view, is to use @swagger_auto_schema decorator (from drf-yasg) above each of your views. It's a bit of a pain but you should be able to find a few post on stackoverflow around using this. An example is:

DRF YASG CustomizingDRF YASG Customizing

Django-Rest-Framework does have a manual method of setting the parameters but most people would prefer to stick to the AutoSchema doing the work. I haven't been able to find much on how to use it for a beginner either. Hence drf-yasg seems a good way to go.

I believe for your post view above the below decorator should be what you're looking for just above the post method:

@swagger_auto_schema(
request_body=PredictionSerializer,
    responses={
        '200': 'OK Request',
        '400': "Bad Request"
    },
)

Also you want to add something like the below to the very top of your view since it will pull through as the description in the schema/documentations.

'''
def post(self, request, format=None):
    "Description of your View"
'''
...
Ryan
  • 33
  • 1
  • 8