从django 1.6升级到1.7可调用,在运行makemigrations时不会被序列化 [英] Upgrading from django 1.6 to 1.7 getting callable is not serialize when running makemigrations

查看:204
本文介绍了从django 1.6升级到1.7可调用,在运行makemigrations时不会被序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们使用可调用的方式动态地生成我们项目中的upload_to目的地,如下所示。

  class PKUploader(object):
def __init __(self,prefix,extension = None):
self.prefix = prefix
self.extension = extension

def __call __(self,instance,filename) :
ext = self.extension或os.path.splitext(filename)[1]
如果不是ext.startswith('。'):
ext ='。'+ ext
如果instance.pk是None:
pk = random.randint(1000,99999)
else:
pk = instance.pk
pk = str(pk)+ str (uuid.uuid4())
return'{} / {} {}'。format(self.prefix,pk,ext)

在我们的模型中,使用像

  class CoolKids(models.Model): 
image = models.ImageField(upload_to = PKUploader('users_image'))

当我们运行升级后m django 1.6到1.7并运行makemigrations我们得到以下错误。

  ValueError:无法序列化:< PKUploader对象在0x7ff5f1cf0b90> 
有一些值Django无法序列化到迁移文件中。
有关更多信息,请参阅https://docs.djangoproject.com/en/dev/topics/migrations/#migration-serializing

任何人都可以看出这一点吗?

解决方案

要序列化任意一个实例class,你需要实现一个 deconstruct()方法:

  class PKUploader(object):
def __init __(self,prefix,extension = None):
self.prefix = prefix
self.extension = extension

def deconstruct自我):
kwargs = {'前缀':self.prefix}
如果self.extension不是无:
kwargs ['extension'] = self.extension
return' import.path.to.PKUploader',(),kwargs

def __call __(self,instance,filename):
...
/ pre>

通过将导入路径返回到类,以及位置和关键字初始化参数,Django可以序列化此信息对有效的python代码重新创建原始实例。



为简化此操作,您可以使用 @deconstructible 装饰器:

  from django.utils.deconstruct import deconstructible 

@deconstructible
class PKUploader(object):
...

请参阅文档


We dynamically generate the upload_to destination in our project using a callable, like below.

class PKUploader(object):
    def __init__(self, prefix, extension=None):
        self.prefix = prefix
        self.extension = extension

    def __call__(self, instance, filename):
        ext = self.extension or os.path.splitext(filename)[1]
        if not ext.startswith('.'):
            ext = '.' + ext
        if instance.pk is None:
            pk = random.randint(1000, 99999)
        else:
            pk = instance.pk
        pk = str(pk) + str(uuid.uuid4())
        return '{}/{}{}'.format(self.prefix, pk, ext)

And in our models it's used like

class CoolKids(models.Model):
    image = models.ImageField(upload_to=PKUploader('users_image'))

However, when we run upgraded from django 1.6 to 1.7 and run makemigrations we get the following error.

ValueError: Cannot serialize: <PKUploader object at 0x7ff5f1cf0b90>
There are some values Django cannot serialize into migration files.
For more, see https://docs.djangoproject.com/en/dev/topics/migrations/#migration-serializing

Can any one shed some light into this?

解决方案

To serialize an instance of an arbitrary class, you need to implement a deconstruct() method:

class PKUploader(object):
    def __init__(self, prefix, extension=None):
        self.prefix = prefix
        self.extension = extension

    def deconstruct(self):
        kwargs = {'prefix': self.prefix}
        if self.extension is not None:
            kwargs['extension'] = self.extension
        return 'import.path.to.PKUploader', (), kwargs

    def __call__(self, instance, filename):
        ...

By returning the import path to the class, and the positional and keyword initialisation arguments, Django can serialize this information to valid python code that recreates the original instance.

To simplify this, you can use the @deconstructible decorator:

from django.utils.deconstruct import deconstructible

@deconstructible
class PKUploader(object):
    ...

See the documentation for full details.

这篇关于从django 1.6升级到1.7可调用,在运行makemigrations时不会被序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆