Upload file in background process in django drf (get this error : Object of type TemporaryUploadedFile is not JSON serializable)

this is my create function:

    def create(self,request,*args,**kwargs):
    
        serializer = ArchiveSerializer( data = request.data, context = {"request":request} )
        serializer.is_valid(raise_exception=True)
        filename=serializer.validated_data.pop('file')
        serializer.save()
        id = serializer.validated_data.get("id")
        save_file_in_background.delay(id,filename)
        
        return Response(serializer.data, status=status.HTTP_201_CREATED)

and this is the tasks.py

from celery import shared_task
from django.core.files.base import ContentFile
from .models import Archive

@shared_task
def save_file_in_background(id,filename):
    try:
        archive = Archive.objects.get(pk=id)

        
        
        if filename:
            archive.file.save(filename, ContentFile(filename))
        

        archive.save()
        
    except Archive.DoesNotExist:
        pass  

but when try to add object and upload the file get this error: Object of type TemporaryUploadedFile is not JSON serializable

except to upload file in background but get this error: "Object of type TemporaryUploadedFile is not JSON serializable"

The error occurs because TemporaryUploadedFile (the uploaded file) can't be passed directly to celery, as it’s not json-serializable. What you can do here is to save the file when instance is creating and pass only ID to avoid serialization.

But I think you want to offload the image posting task without blocking the main request - response cycle.

You can do this by first saving Minimal data only the basic details (excluding the image) and return a response to the client. And then pass any required information to the Celery task to handle the image upload.

from rest_framework import status
from rest_framework.response import Response
from .tasks import upload_image_background_task

def create(self, request, *args, **kwargs):
    serializer = ArchiveSerializer(data=request.data, context={"request": request})
    serializer.is_valid(raise_exception=True)

    # initially save the instance without image
    archive_instance = serializer.save(file=None)

    # take the uploaded image file from the request
    uploaded_image = request.FILES.get('file')

    # pass the instance id and the image data to the background task
    upload_image_background_task.delay(archive_instance.id, uploaded_image.read(), 
uploaded_image.name)

    # return response immediately
    return Response(serializer.data, status=status.HTTP_201_CREATED)

in your tasks.py file modify:

@shared_task
def upload_image_background_task(archive_id, image_data, image_name):
    try:
        archive = Archive.objects.get(pk=archive_id)
        
        # save the image file in the background
        archive.file.save(image_name, ContentFile(image_data))
        
        # save the instance to finalize changes
        archive.save()
        
    except Archive.DoesNotExist:
        pass  # handle the case where the instance might not exist
Back to Top