我正在尝试设置 Cloudinary 的上传小部件并将生成的 Cloudinary 对象保存到CloudinaryField()我的模型中。Cloudinary 的示例项目仅展示了如何使用{{form}}.
当我使用小部件上传时,我得到了一本字典,但我在文档中找不到任何地方可以让我保存它。
小智 6
上传小部件可以选择包含通知 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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
319 次 |
| 最近记录: |