Django 1.7 makemigrations - ValueError:无法序列化类

时间:2015-07-03 22:56:06

标签: python django python-2.7

从Django 1.6.11升级到1.7,我遇到了一个有趣的问题。它似乎是基于我目前如何拆分文件。目前,由于大量的方法,模型方法存储在与模型不同的文件中。

例如,它按如下方式拆分:

help
|_ modelmethods
|  |_ __init__.py
|  |_ thread_methods.py
|_ __init__.py
|_ models.py

帮助应用程序文件夹中的__init__.py如下所示:

""" __init__.py for help app."""

from help.modelmethods.thread_methods import *

而thread_methods.py看起来像这样:

"""Methods for the Thread model."""

from help.models import Thread

class ThreadMethods:

    """Adds methods on to the Thread model."""

    def do_the_thing(self):
        pass

Thread.__bases__ += (ThreadMethods,)

我从中看到的错误如下:

Migrations for 'help':
  0001_initial.py:
    - Create model Thread
Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/user/.virtualenvs/stuff/lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
    utility.execute()
  File "/Users/user/.virtualenvs/stuff/lib/python2.7/site-packages/django/core/management/__init__.py", line 377, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/user/.virtualenvs/stuff/lib/python2.7/site-packages/django/core/management/base.py", line 288, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/Users/user/.virtualenvs/stuff/lib/python2.7/site-packages/django/core/management/base.py", line 338, in execute
    output = self.handle(*args, **options)
  File "/Users/user/.virtualenvs/stuff/lib/python2.7/site-packages/django/core/management/commands/makemigrations.py", line 124, in handle
    self.write_migration_files(changes)
  File "/Users/user/.virtualenvs/stuff/lib/python2.7/site-packages/django/core/management/commands/makemigrations.py", line 152, in write_migration_files
    migration_string = writer.as_string()
  File "/Users/user/.virtualenvs/stuff/lib/python2.7/site-packages/django/db/migrations/writer.py", line 129, in as_string
    operation_string, operation_imports = OperationWriter(operation).serialize()
  File "/Users/user/.virtualenvs/stuff/lib/python2.7/site-packages/django/db/migrations/writer.py", line 86, in serialize
    arg_string, arg_imports = MigrationWriter.serialize(arg_value)
  File "/Users/user/.virtualenvs/stuff/lib/python2.7/site-packages/django/db/migrations/writer.py", line 245, in serialize
    item_string, item_imports = cls.serialize(item)
  File "/Users/user/.virtualenvs/stuff/lib/python2.7/site-packages/django/db/migrations/writer.py", line 380, in serialize
    raise ValueError("Cannot serialize: %r\nThere are some values Django cannot serialize into migration files.\nFor more, see https://docs.djangoproject.com/en/dev/topics/migrations/#migration-serializing" % value)
ValueError: Cannot serialize: <class help.modelmethods.thread_methods.ThreadMethods at 0x1105c3870>
There are some values Django cannot serialize into migration files.
For more, see https://docs.djangoproject.com/en/dev/topics/migrations/#migration-serializing

我意识到它正在尝试序列化类并使其窒息。有没有一种好方法可以解决这个问题并保持分离?或者唯一可比的方法是将models.py文件拆分为具有正确__init__.py设置的模型文件夹,并且每个文件专用于一个模型,该模型还包含所有相关方法(以及制作确定没有引入循环导入。)

3 个答案:

答案 0 :(得分:2)

由于自定义验证程序,我无法迁移。我的问题是我没有正确阅读the manual,其中说:

  

如果在验证器模型字段中使用基于类的验证器   选项,您应该确保它可以通过迁移进行序列化   框架通过添加deconstruct()__eq__()方法。

哪个指向migrations-docs,它解释了为什么需要deconstruct()__eq__()以及如何编写它们。

也应该适用于其他类,而不仅仅适用于验证器。

答案 1 :(得分:1)

您需要从 object 类派生方法模型,同时尝试从ThreadMethods派生Thread而不是将其添加到 __ bases __

class ThreadMethods(object):
    # ....

答案 2 :(得分:1)

这种情况可能由于多种原因而发生,在我的情况下,我为default=User.pk设置了user这导致了问题。我的django版本是1.9

class Blog(models.Model): title = models.CharField(max_length=200) content = HTMLField() pub_date = models.DateTimeField('date published', auto_now_add=True) last_updated = models.DateTimeField('date published',default=timezone.now) user = models.ForeignKey(User, default=User.pk)#wrong user = models.ForeignKey(User, default=1)#correct, use any default value featured = models.ImageField(upload_to = 'featured', blank=True)