我正在尝试将一些模型 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
那么,如何移动/更改模型实例上的文件存储?