我正在尝试设置 Cloudinary 的上传小部件并将生成的 Cloudinary 对象保存到我的模型上的 CloudinaryField()
中。 Cloudinary的sample project仅显示如何使用 {{form}}
上传图片。
当我使用小部件上传时,我得到了一本字典,但我在文档中找不到任何地方可以让我保存它。
最佳答案
上传小部件可以选择包含通知 URL,并且可以在您的上传预设设置中进行配置。此通知 URL 将返回对您的服务器端点的响应,可用于保存图像模型。
要从上传响应生成必要的 CloudinaryField,可以使用 CloudinaryResource 对象。
from cloudinary import CloudinaryResource
. . .
json_response = { "public_id": "test", "type": "upload", . . . }
# Populate a CloudinaryResource object using the upload response
result = CloudinaryResource(public_id=json_response['public_id'], type=json_response['type'], resource_type=json_response['resource_type'], version=json_response['version'], format=json_response['format'])
str_result = result.get_prep_value() # returns a CloudinaryField string e.g. "image/upload/v123456789/test.png"
# Save the result
p = Photo(title="title",image=str_result)
p.save() # save result in database
关于Django - 通过上传小部件保存 Cloudinary 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46736593/