【发布时间】:2017-03-31 05:02:22
【问题描述】:
我正在尝试对我的文件上传 REST API 进行单元测试。我在网上找到了一些使用 Pillow 生成图像的代码,但无法序列化。
这是我生成图像的代码:
image = Image.new('RGBA', size=(50, 50), color=(155, 0, 0))
file = BytesIO(image.tobytes())
file.name = 'test.png'
file.seek(0)
然后我尝试上传这张图片:
return self.client.post("/api/images/", data=json.dumps({
"image": file,
"item": 1
}), content_type="application/json", format='multipart')
我收到以下错误:
<ContentFile: Raw content> is not JSON serializable
如何转换 Pillow 图像以使其可序列化?
【问题讨论】:
-
您是否尝试过省略
json.dumps电话?在我的 django 项目中,我只是使用测试客户端将数据作为字典发布。
标签: django rest django-rest-framework