我正在尝试将一些模型 ImageFields 迁移到使用 S3BotoStorage
来自 django-storages
的存储后端.作为此过程的一部分,我更改了模型的 ImageField 声明以包含 storage=instance_of_s3botostorage
参数,并且我的模型的新实例将图像保存到 ImageField 属性现在存储在 S3 中 - 按预期。
我也尝试将现有模型实例转移到 S3 中存储它们的数据,所以写了一个像这样的 South DataMigration:
def forwards(self, orm):
"upload ImageField file to S3 if it's not already in there"
for mymodel in orm.MyModel.objects.all():
if mymodel.logo_image and not isinstance(mymodel.logo_image.storage, S3BotoStorage):
print "uploading %s to S3" % mymodel.logo_image
file_contents = ContentFile(mymodel.logo_image.read())
mymodel.logo_image.save(mymodel.logo_image.name, file_contents)
mymodel.save()
但这显然没有达到预期的效果,因为图像文件只是使用旧的
storage
保存的。后端 - 考虑到 save() 实际上是 FieldFile
的一种方法,这是有道理的属于FileField
那么,如何移动/更改模型实例上的文件存储?
最佳答案
因此,用于文件的特定存储并未存储在数据库中。 “迁移”只是更改模型定义的问题,然后在使用存储子系统 API 之外,只需将文件上传到新的存储位置。
关于django - 如何使用 South 的 DataMigration 更改 Django 模型 ImageField 实例的存储后端?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3545005/