23

我正在使用 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'
4

4 回答 4

42

Django 1.7.1 在 AlterField 中添加了对 preserve_default 参数的支持。因此,从 1.7 版升级 Django 将解决该问题。

pip install django --upgrade python manage.py migrate

于 2014-11-02T15:48:20.157 回答
7

有同样的问题。我解决了它:

pip install django --upgrade

python manage.py makemigrations

python manage.py migrate

为什么它会这样解决,我不知道,有更深入的知识的人可能能够解释......

于 2014-10-23T08:28:29.510 回答
1
pip install "django<1.8" -U

然后运行迁移....

于 2015-05-29T18:38:32.540 回答
0

我遇到了同样的问题。我发现我的 Django 版本是 1.7.0。运行后pip install django --upgrade,我的django版本改成了1.7.4,问题就没有了。

我记得另一台通过 Internet 与这台同步项目文件的计算机具有 Django 版本 1.7.3。所以我得出的结论是这个问题是由Django的版本不一致引起的。

于 2015-01-28T15:17:14.640 回答