Photo by Annie Spratt on Unsplash

Django rest framework: How to make a simple file upload API using viewsets.

This article is to show you how easy it is to make a simple file upload API using Django viewsets. There are other alternatives to do the same but in this article, we will be using a viewsets, a serializer, and Django default routing to make an API to upload any file.

This article is just to shows how to make an API to upload a file and it is for starters, so the code does all the taking for this article. Just go through the source code to get more information.

Serializers.py

This is the part where we define the FileField and we will be using a Serializer to serialize the request which has the file from our API.

Views.py

We will be using the ViewSet and Serializer to make the API for file upload.

Urls.py

In this example, we are using a DefaultRouter which adds support for automatic URL routing to Django, and provides you with a simple, quick, and consistent way of wiring your view logic to a set of URLs.

API Root

Working

When you got to localhost:8000 you will see the API Root and on clicking the URL you will be redirected to the API page. So what I have done in my views is when a get request is sent it will give you a response saying GET API.

GET API response
POST API response
The upload part

Works at @impressAI