我已经阅读了Cloudinary Django 文档和许多StackOverflow问题,但我仍然在努力解决这个基本问题。我想将图像作为数据迁移的一部分上传到Cloudinary模型字段(类
我的模型定义如下:
这个模型运行良好,我可以在管理界面上传图片并在模板中访问它,但是我想编写数据迁移程序以便我可以向此模型上传多个图片。我的迁移程序无法正常工作,结构如下:
我知道如果我使用
CloudinaryField
)中。我的模型定义如下:
# model definition
from cloudinary.models import CloudinaryField
from django.contrib.gis.db import models
class UserProfile(models.Model):
name = models.CharField(max_length=200)
logo = CloudinaryField(blank=True, null=True)
这个模型运行良好,我可以在管理界面上传图片并在模板中访问它,但是我想编写数据迁移程序以便我可以向此模型上传多个图片。我的迁移程序无法正常工作,结构如下:
# migration file to upload logos
import cloudinary
# get all user instances
users = UserProfile.objects.all()
# loop through users, and update logo
for user in users:
user.logo = cloudinary.uploader.upload("https://url/to/logo/logo.png")
user.save()
我知道如果我使用
cloudinary.uploader.upload("image.png")
,会得到类似下面的结果:{u'secure_url': u'https://res.cloudinary.com/mysite/image/upload/111/111.png', u'public_id': 111', u'format': u'png', u'url': u'http://res.cloudinary.com/mysite/image/upload/v1444253137/111.png', u'created_at': u'2015-10-07T21:25:37Z', u'tags': [], u'bytes': 7974, u'height': 35, u'width': 290, u'version': 1444253137, u'etag': u'aaa', u'original_filename': u'logo', u'signature': u'111', u'type': u'upload', u'resource_type': u'image'}
我无法确定是否可以使用Cloudinary将上传的文件与模型字段相关联。所有文档(和示例代码)都没有显示如何将上传响应与模型字段相关联。有一些使用Web表单的示例,但如果可能的话,我更愿意避免使用它。
upload_resource
解释,只有这篇文章在一年后才解释清楚了。 - raratiru