2
votes

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.

1
By "dictionary", did you mean the identifier string that's the value of the generated hidden input field, or the JSON response that's returned by the POST request?Itay Taragano
It's the set of values returned by the POST request to Cloudinary. I suppose it is JSON, but I am sending it straight to the backend and Python seems to be recognizing it as a dictionary. Maybe I am skipping a step?Adam Starrh

1 Answers

6
votes

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