Last modified: Mar 10, 2025 By Alexander Williams
Handling File Uploads in Django Rest Framework
Handling file uploads is a common requirement when building APIs. Django Rest Framework (DRF) makes it easy to manage file uploads. This guide will walk you through the process.
Setting Up Django Rest Framework
Before handling file uploads, ensure DRF is installed. Use pip install djangorestframework
to install it. Add 'rest_framework'
to your INSTALLED_APPS
in settings.py
.
INSTALLED_APPS = [
...
'rest_framework',
...
]
For more on setting up DRF, check our guide on Setting Up Your First API with Django Rest Framework.
Creating a Model for File Uploads
First, create a model to store uploaded files. Use Django's FileField
or ImageField
for this purpose.
from django.db import models
class UploadedFile(models.Model):
file = models.FileField(upload_to='uploads/')
uploaded_at = models.DateTimeField(auto_now_add=True)
This model will store files in the uploads/
directory. Run python manage.py makemigrations
and python manage.py migrate
to apply changes.
Creating a Serializer
Next, create a serializer for the UploadedFile
model. Serializers convert complex data types into Python native types.
from rest_framework import serializers
from .models import UploadedFile
class UploadedFileSerializer(serializers.ModelSerializer):
class Meta:
model = UploadedFile
fields = ['id', 'file', 'uploaded_at']
For more on serializers, read our guide on Django Rest Framework: Serializers and ModelSerializer.
Creating a View for File Uploads
Now, create a view to handle file uploads. Use DRF's APIView
or ModelViewSet
for this purpose.
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status
from .models import UploadedFile
from .serializers import UploadedFileSerializer
class FileUploadView(APIView):
def post(self, request, *args, **kwargs):
file_serializer = UploadedFileSerializer(data=request.data)
if file_serializer.is_valid():
file_serializer.save()
return Response(file_serializer.data, status=status.HTTP_201_CREATED)
return Response(file_serializer.errors, status=status.HTTP_400_BAD_REQUEST)
This view handles POST requests and saves uploaded files. It returns the file data if successful or errors if not.
Configuring URLs
Finally, configure URLs to access the file upload view. Add the following to your urls.py
.
from django.urls import path
from .views import FileUploadView
urlpatterns = [
path('upload/', FileUploadView.as_view(), name='file-upload'),
]
Now, you can upload files by sending a POST request to /upload/
.
Testing the File Upload
Use tools like Postman or cURL to test the file upload. Here’s an example using cURL.
curl -X POST -F "file=@/path/to/your/file.txt" http://localhost:8000/upload/
If successful, the API will return the file details in JSON format.
Handling Permissions
You may want to restrict file uploads to authenticated users. Use DRF's permissions for this.
from rest_framework.permissions import IsAuthenticated
class FileUploadView(APIView):
permission_classes = [IsAuthenticated]
...
For more on permissions, check our guide on Using Permissions in Django Rest Framework.
Conclusion
Handling file uploads in Django Rest Framework is straightforward. By following this guide, you can easily add file upload capabilities to your API. Start building powerful APIs today!