django - 如何使用 South 的 DataMigration 更改 Django 模型 ImageField 实例的存储后端?

标签 django migration django-south imagefield django-storage

我正在尝试将一些模型 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/

相关文章:

java - 从 tomcat 迁移到 websphere

Django 南 : How to create rules for a Custom Field?

带有 Django 1.7 迁移的 Python 2.7 未绑定(bind)方法

django - 向数据库添加初始数据

python - Django 。显示正确的时间 - 取决于时区

python - django.db.utils.IntegrityError : column "color_set_id" contains null values

django - 为什么 Django (1.5.1) 会调用 Loader.load_template_source() 69 次?

mysql - 在 Django 中使用带有 GROUP BY 子句的 COUNT(DISTINCT 字段)

java - 如何从 Jdeveloper w ADF 转换/迁移到替代/优雅的 IDE (Eclipse)

ruby-on-rails - rails 3.1 分贝 :test:prepare and "pending migrations" problem