Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 871
Tough Guy Avatar asked Jan 25 '23 23:01

Tough Guy


2 Answers

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

like image 177
Blux Avatar answered Jan 28 '23 14:01

Blux


Check out this issue. You can find how to use @swagger_auto_schema to create something like this

enter image description here

like image 36
Silvio Messi Avatar answered Jan 28 '23 13:01

Silvio Messi