0
votes

I have a model:

from cloudinary.models import CloudinaryField

class Image(models.Model):
    image = CloudinaryField('image')

And an object that I received from Cloudinary. It was given to me by Cloudinary after I uploaded an image, and I passed it back to my server:

{'etag': ['***'], 'version': ['***'], 'public_id': ['***'], 'type': ['upload'], 'signature': ['***'], 'created_at': ['2017-07-28T15:54:17Z'], 'secure_url': ['https://res.cloudinary.com/***/***.png'], 'height': ['353'], 'format': ['png'], 'resource_type': ['image'], 'url': ['http://res.cloudinary.com/***/image/upload/***/***.png'], 'width': ['684'], 'bytes': ['133509']}

I can't seem to save it to my model. I have tried both saving it directly and putting it through the CloudinaryJsFileField form in Cloudinary's documentation. Neither seems to work. The form laments that "No File Was Selected" and the model.create() effort complains of a missing property.

This is the form I am using:

class PhotoDirectForm(ModelForm):

    class Meta:
        model = Image
        fields = ['image']

I've tried it both with and without CloudinaryJsFileField, and I get the same error either way:

<tr><th><label for="id_image">Image:</label></th><td><ul class="errorlist"><li>No file selected!</li></ul><input type="file" name="image" required id="id_image" /></td></tr>

Obviously it wants a file, but that's not what Cloudinary gives me after I upload my image!

Does anyone know how to save Cloudinary objects to the Django database?

1

1 Answers

0
votes

Did you check the Photo album sample project?

The flow there is -

views.py -

form = PhotoForm(request.POST, request.FILES)
...
form.save()

forms.py -

class PhotoForm(ModelForm):
class Meta:
    model = Photo
    fields = '__all__'

models.py -

class Photo(models.Model):
## Misc Django Fields
create_time = models.DateTimeField(auto_now_add=True)
title = models.CharField("Title (optional)", max_length=200, blank=True)

## Points to a Cloudinary image
image = CloudinaryField('image')

This should save the image both in Cloudinary and your storage.