如何使用South的DataMigration来更改Django模型ImageField实例的存储后端?
我正在尝试将一些模型ImageFields迁移到使用
django-storages
的S3BotoStorage
存储后端。作为这个过程的一部分,我已经改变了我的Model的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
那么,如何在模型实例上移动/更改文件存储?
没有找到相关结果
已邀请:
2 个回复
膛嵌墒缅欠
览幕堤分