djangoswaggerdrf-yasg

How to make swagger schema for file upload API in django-rest-framework using drf-yasg?


I am not able to find any support for making a schema for the file upload API. The Swagger UI must have a button allowing a tester to upload a file for testing purposes. I am using firebase as a database so serializers and models don't come into the picture. I am using only Django's rest framework.

I have looked at drf-yasg's documentation that suggests using Operation for file upload. But It is a very abstract and obscure documentation.


Solution

  • Make sure you specify the parser_classes in your view. By Default it's JSON parser which doesn't handle file uploads. Use either MultiPartParser or FileUploadParser

    class MyUploadView(CreateAPIView):
        parser_classes = (MultiPartParser,)
        ...
    
        @swagger_auto_schema(operation_description='Upload file...',)
        @action(detail=False, methods=['post'])
        def post(self, request, **kwargs):
            # Code to handle file