升级到Django 1.7。获取错误:无法序列化:

时间:2014-09-03 15:32:57

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

我正在尝试将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上。任何帮助表示赞赏。

2 个答案:

答案 0 :(得分:27)

只需制作一个可解构的子类,然后使用它。

from django.utils.deconstruct import deconstructible


@deconstructible
class MyS3BotoStorage(S3BotoStorage):
    pass

答案 1 :(得分:6)

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

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

  

迁移只是包含模型的旧定义的Python文件 - 因此,为了编写它们,Django必须采用模型的当前状态并将它们序列化为文件。

     

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

     

您可以让Django通过为类提供deconstruct()方法来序列化您自己的自定义类实例。

因此,此处的解决方案是为类storages.backends.s3boto.S3BotoStorage提供deconstruct()方法。这可能就像应用@deconstructible类装饰器一样简单。

据推测,软件包会在某个时候包含这个变化(或者主分支已经拥有它?),但你也可以自己修补它。