我正在尝试在我的网络应用程序中使用芹菜,但我不知道具体如何。我在任务中取出图像更新,他不断抱怨序列化程序
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
您可以通过这种方式传递 Image 对象,传递图像的路径并在任务中打开它。
task = update_image.delay(self.image.path, self.width, self.heigth)
任务:
def update_image(path, width, height):
image = Image.open(path)
...