Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django 1.7迁移无法序列化类方法_Python_Django - Fatal编程技术网

Python Django 1.7迁移无法序列化类方法

Python Django 1.7迁移无法序列化类方法,python,django,Python,Django,我有一个非常基本的类,它看起来类似于以下内容: class Car(Model): name = CharField(max_length=255, unique=True) @classmethod def create_simple_examples(cls): for c in ['Sedan', 'Coupe', 'Van', 'SUV']: cls.objects.get_or_create(name=c)

我有一个非常基本的类,它看起来类似于以下内容:

class Car(Model):

    name = CharField(max_length=255, unique=True)

    @classmethod
    def create_simple_examples(cls):
        for c in ['Sedan', 'Coupe', 'Van', 'SUV']:
            cls.objects.get_or_create(name=c)

    @classmethod
    def get_default(cls):
        c, _ = cls.objects.get_or_create(name='Sedan')
        return c

    def __unicode__(self):
        return self.name
我正在尝试将其添加到django应用程序中。我有两个类的方法。一个用于快速填充表的函数,以及2。获取一个经常使用的默认值

当我跑的时候

python manage.py makemigrations myapp
我得到以下错误

ValueError: Cannot serialize: <bound method ModelBase.get_default of <class 'crunch.django.myapp.models.Car'>>
因此,迁移似乎正在尝试序列化函数。关于如何绕过此问题的任何提示?

如中所述,Django可以序列化函数和方法引用,(在Python 3中)从类体中使用的未绑定方法,以及一系列其他内容,但它不能序列化所有内容

在本例中,由于您已将
get_default
设置为
@classmethod
Car.get_default
是一个绑定方法(即,它将对
Car
的隐式引用作为其第一个参数),而不是一个普通的函数或方法引用,Django不知道如何处理它


尝试将
get\u default
改为
@staticmethod
,或者创建一个调用
Car的自由函数(顶级函数)。get\u default

@deconstructible
装饰器添加到具有
classmethod

from django.utils.deconstruct import deconstructible

@deconstructible
class Car(Model):
    ...

更多关于的文档可以在这里找到

尝试将默认值移动到保存方法或post_save Etch如何处理,最后?我遇到了同样的问题,并使用
classmethod
s将此装饰器添加到类中,但Django在迁移时仍然给出相同的错误。。。原因可能是什么?您是否已清除过时的pyc文件并重试?我还可以想象下一次发生错误的情况。如果不看stacktrace等,就很难进行推测。Custom deconstruct()方法,请参阅使其成为收费函数为我修复了它。
from django.utils.deconstruct import deconstructible

@deconstructible
class Car(Model):
    ...