将Cloudinary对象保存到Django模型

时间:2021-05-14 23:48:50

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:

还有一个我从Cloudinary收到的对象。我上传了一张图片后,它被Cloudinary给了我,然后我把它传回了我的服务器:

{'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.

我似乎无法将它保存到我的模型中。我已经尝试过直接保存它并将它放在Cloudinary的文档中的CloudinaryJsFileField表单中。似乎都没有用。该表格感叹“没有选择文件”,而model.create()工作则抱怨缺少属性。

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:

无论是否使用CloudinaryJsFileField我都尝试过,并且无论如何都会得到相同的错误:

<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!

显然它想要一个文件,但这不是Cloudinary在我上传图像后给我的东西!

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

有谁知道如何将Cloudinary对象保存到Django数据库?

1 个解决方案

#1


0  

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.

这应该在Cloudinary和您的存储中保存图像。

#1


0  

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.

这应该在Cloudinary和您的存储中保存图像。