我按照说明将Django应用程序从Django 1.6.11升级到1.8.1。除了 init .py之外,我已删除了所有迁移功能。然后我跑:
manage.py makemigrations
它崩溃了以下追溯:
Traceback (most recent call last):
File "manage.py", line 12, in <module>
execute_from_command_line(sys.argv)
File "/Users/dwatson/Environments/website/lib/python2.7/site-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
utility.execute()
File "/Users/dwatson/Environments/website/lib/python2.7/site-packages/django/core/management/__init__.py", line 330, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Users/dwatson/Environments/website/lib/python2.7/site-packages/django/core/management/base.py", line 390, in run_from_argv
self.execute(*args, **cmd_options)
File "/Users/dwatson/Environments/website/lib/python2.7/site-packages/django/core/management/base.py", line 441, in execute
output = self.handle(*args, **options)
File "/Users/dwatson/Environments/website/lib/python2.7/site-packages/django/core/management/commands/makemigrations.py", line 143, in handle
self.write_migration_files(changes)
File "/Users/dwatson/Environments/website/lib/python2.7/site-packages/django/core/management/commands/makemigrations.py", line 171, in write_migration_files
migration_string = writer.as_string()
File "/Users/dwatson/Environments/website/lib/python2.7/site-packages/django/db/migrations/writer.py", line 166, in as_string
operation_string, operation_imports = OperationWriter(operation).serialize()
File "/Users/dwatson/Environments/website/lib/python2.7/site-packages/django/db/migrations/writer.py", line 124, in serialize
_write(arg_name, arg_value)
File "/Users/dwatson/Environments/website/lib/python2.7/site-packages/django/db/migrations/writer.py", line 75, in _write
arg_string, arg_imports = MigrationWriter.serialize(item)
File "/Users/dwatson/Environments/website/lib/python2.7/site-packages/django/db/migrations/writer.py", line 303, in serialize
item_string, item_imports = cls.serialize(item)
File "/Users/dwatson/Environments/website/lib/python2.7/site-packages/django/db/migrations/writer.py", line 377, in serialize
return cls.serialize_deconstructed(path, args, kwargs)
File "/Users/dwatson/Environments/website/lib/python2.7/site-packages/django/db/migrations/writer.py", line 268, in serialize_deconstructed
arg_string, arg_imports = cls.serialize(arg)
File "/Users/dwatson/Environments/website/lib/python2.7/site-packages/django/db/migrations/writer.py", line 465, in serialize
"topics/migrations/#migration-serializing" % (value, get_docs_version())
ValueError: Cannot serialize: <bound method AccountManager.gen_num of <coreapi.models.AccountManager object at 0x103925450>>
There are some values Django cannot serialize into migration files.
For more, see https://docs.djangoproject.com/en/1.8/topics/migrations/#migration-serializing
此函数是AccountManager类中定义的方法,它派生自models.Manager:
def gen_num(self):
max_digits = 9
candidate = random.randint(0, 10**max_digits)
return candidate
我已经检查过返回候选行,并且正如预期的那样,它是一个int。
我假设此方法正在被内省,因为它被设置为关联字段的默认值:
num = models.PositiveIntegerField(
_('Num'), help_text=_('Number, automatically-generated'),
unique=True, blank=True, null=True, default=AccountManager().gen_num
)
答案 0 :(得分:0)
解决方案是遵循这里给出的建议:
https://docs.djangoproject.com/en/1.8/topics/migrations/#migration-serializing
也就是说,将函数从Manager的类体移动到模块体:
def gen_num():
max_digits = 9
candidate = random.randint(0, 10**max_digits)
return candidate
完成此更改后,makemigrations成功完成。