类型为JpegImageFile的对象不可JSON序列化

时间:2020-06-10 18:57:13

标签: django celery

我正在尝试在Web应用程序中使用celery,但我不知道具体如何。我在任务中取出图像更新,他不断抱怨串行器

tasks.py

from celery import shared_task
@shared_task
def update_image(image, width, heigth):
    output_size = (width, heigth)
    image.thumbnail(output_size)
    image.save()

models.py:

def save(self):
    img = Image.open(self.image.path)
    task = update_image.delay(img, self.width, self.heigth)

views.py:

 def put(self, request, pk):
        saved_content = get_object_or_404(Content.objects.all(), pk=pk)
        data = request.data.get('content')
        serializer = ContentSerializer(
            instance=saved_content, data=data, partial=True)
        if serializer.is_valid(raise_exception=True):
            content_saved = serializer.save()
        return Response({
            "success": "Picture '{}' updated successfully".format(content_saved.id)
        })

错误:

Object of type JpegImageFile is not JSON serializable

Request Method: PUT

1 个答案:

答案 0 :(得分:1)

您可以以这种方式传递Image对象,传递图像的路径,然后在任务内部打开它。

task = update_image.delay(self.image.path, self.width, self.heigth)

任务:

def update_image(path, width, height):
    image = Image.open(path)
    ...