Reputation: 6958
I am trying to set up Cloudinary's upload widget and save the resulting Cloudinary objet to my CloudinaryField()
on my model. Cloudinary's sample project only shows how to upload images using {{form}}
.
When I upload using the widget, I get a dictionary back, but I can't find any place in the docs where it gives me a way to save it.
Upvotes: 2
Views: 613
Reputation: 497
The upload widget has the option to include a notification URL and that can be configured in your upload preset settings. This notification URL will return a response to your server endpoint that can be used to save the image model.
To generate the necessary CloudinaryField from the upload response, a CloudinaryResource object can be used.
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
Upvotes: 6