Django 1.7 迁移
Posted
技术标签:
【中文标题】Django 1.7 迁移【英文标题】:Django 1.7 Migrations 【发布时间】:2014-12-13 11:07:22 【问题描述】:我正在使用 django 1.7,我刚刚添加了一个自定义用户模型。当我运行python3 manage.py makemigrations
或python3 manage.py migrate
时,我得到错误:TypeError: __init__() got an unexpected keyword argument 'preserve_default'
。添加新的自定义用户模型后出现此问题。完整的回溯是:
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/usr/local/lib/python3.4/dist-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python3.4/dist-packages/django/core/management/__init__.py", line 377, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/local/lib/python3.4/dist-packages/django/core/management/base.py", line 288, in run_from_argv
self.execute(*args, **options.__dict__)
File "/usr/local/lib/python3.4/dist-packages/django/core/management/base.py", line 338, in execute
output = self.handle(*args, **options)
File "/usr/local/lib/python3.4/dist-packages/django/core/management/commands/migrate.py", line 63, in handle
executor = MigrationExecutor(connection, self.migration_progress_callback)
File "/usr/local/lib/python3.4/dist-packages/django/db/migrations/executor.py", line 17, in __init__
self.loader = MigrationLoader(self.connection)
File "/usr/local/lib/python3.4/dist-packages/django/db/migrations/loader.py", line 48, in __init__
self.build_graph()
File "/usr/local/lib/python3.4/dist-packages/django/db/migrations/loader.py", line 173, in build_graph
self.load_disk()
File "/usr/local/lib/python3.4/dist-packages/django/db/migrations/loader.py", line 103, in load_disk
migration_module = import_module("%s.%s" % (module_name, migration_name))
File "/usr/lib/python3.4/importlib/__init__.py", line 109, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 2231, in _gcd_import
File "<frozen importlib._bootstrap>", line 2214, in _find_and_load
File "<frozen importlib._bootstrap>", line 2203, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 1200, in _load_unlocked
File "<frozen importlib._bootstrap>", line 1129, in _exec
File "<frozen importlib._bootstrap>", line 1448, in exec_module
File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed
File "/home/denny/workspace/teenvestor/core/migrations/0003_auto_20141017_1749.py", line 7, in <module>
class Migration(migrations.Migration):
File "/home/denny/workspace/teenvestor/core/migrations/0003_auto_20141017_1749.py", line 46, in Migration
preserve_default=True,
TypeError: __init__() got an unexpected keyword argument 'preserve_default'
【问题讨论】:
完整的回溯会有很大帮助。您还安装了哪个 Django 1.7 版本? (例如 1.7.1) 刚刚编辑了完整的回溯。不确定1.7的具体版本...我刚刚做了一个pip install django==1.7 【参考方案1】:Django 1.7.1 在 AlterField 中添加了对 preserve_default 参数的支持。因此,从 1.7 版升级 Django 将解决此问题。
pip install django --upgrade
python manage.py migrate
【讨论】:
您也可以手动编辑迁移文件以删除 preserve_default 参数。【参考方案2】:遇到了同样的问题。我解决了它:
pip install django --upgrade
python manage.py makemigrations
python manage.py migrate
究竟为什么会这样解决,我不知道,可能有更深入的知识的人能够解释......
【讨论】:
可能是因为 django 在 10 月 22 日更新到 1.7.1 ?【参考方案3】:pip install "django<1.8" -U
然后运行迁移....
【讨论】:
【参考方案4】:我遇到了同样的问题。我发现我的 Django 版本是 1.7.0。
运行pip install django --upgrade
后,我的Django版本改成1.7.4
,问题就没有了。
我记得另一台通过 Internet 与这台同步项目文件的计算机具有 Django 版本 1.7.3。所以我得出的结论是这个问题是由Django的版本不一致引起的。
【讨论】:
以上是关于Django 1.7 迁移的主要内容,如果未能解决你的问题,请参考以下文章
Django 1.7 中的 Django-migrations 检测模型更改,但不会在迁移时应用它们