【问题标题】:Upgrading to Django 1.7. Getting error: Cannot serialize: <storages.backends.s3boto.S3BotoStorage object升级到 Django 1.7。出现错误:无法序列化:<storages.backends.s3boto.S3BotoStorage 对象
【发布时间】:2014-10-28 04:35:16
【问题描述】:

我正在尝试将 django 应用程序从 django 1.6.6 升级到 1.7,并且正在使用 python 2.7.8。当我运行python manage.py makemigrations 时,出现以下错误:

ValueError: Cannot serialize: <storages.backends.s3boto.S3BotoStorage object at 0x11116eed0>
There are some values Django cannot serialize into migration files.

这里是相关代码:

protected_storage = storages.backends.s3boto.S3BotoStorage(
      acl='private',
      querystring_auth=True,
      querystring_expire=3600,
    )


    class Document(models.Model):
        ...
        file = models.FileField(upload_to='media/docs/', max_length=10000, storage=protected_storage)

        def __unicode__(self):
            return "%s" % self.candidate

        def get_absolute_url(self):
            return reverse('documents', args=[str(self.pk)])

我已阅读迁移文档并阅读了类似问题 here,但我无法解决此问题。我的应用程序使用 django-storages 和 boto 将文件保存到 Amazon S3。任何帮助表示赞赏。

【问题讨论】:

    标签: python django python-2.7 boto django-storage


    【解决方案1】:

    只需创建一个可解构的子类并使用它。

    from django.utils.deconstruct import deconstructible
    
    
    @deconstructible
    class MyS3BotoStorage(S3BotoStorage):
        pass
    

    【讨论】:

    • from django.utils.deconstruct import deconstructible
    【解决方案2】:

    这里的基本问题是,您正在尝试将 Django 1.7 与似乎尚未更新以适用于该版本的包 (django-storages) 一起使用。

    以下是documentation 的一些摘录,用于解释正在发生的事情:

    迁移只是包含模型旧定义的 Python 文件 - 因此,要编写它们,Django 必须获取模型的当前状态并将它们序列化到一个文件中。

    虽然 Django 可以序列化大多数东西,但有些东西我们无法序列化为有效的 Python 表示 - 没有关于如何将值转回代码的 Python 标准。

    你可以让 Django 序列化你自己的自定义类实例,方法是给类一个 deconstruct() 方法。

    所以这里的解决方案是给类storages.backends.s3boto.S3BotoStorage一个deconstruct()方法。这可能就像应用 @deconstructible 类装饰器一样简单。

    大概这个包会在某个时候包含这个变化(或者也许主分支已经有了它?),但你也可以自己修补它。

    【讨论】:

    • @deconstructible class MyS3BotoStorage(S3BotoStorage): pass
    • 关于 django-storages 的评论为我修复了它。我升级了,事情就解决了。在我的情况下,使用 @deconstructible 没有。
    • 对于自定义 deconstruct() 方法 -- 见stackoverflow.com/questions/31953802/…
    猜你喜欢
    • 2014-08-05
    • 1970-01-01
    • 1970-01-01
    • 2014-10-06
    • 2015-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多