我需要为我的应用创建一个自定义用户,并且完全遵循我AUTH_USER_MODEL = 'core.MyUser'
中settings.py
的文档中的example。但是,当我创建一个新数据库时,删除所有迁移文件夹并再次运行python manage.py migrate
,它会给我这样的例外
File "manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "/Users/bubuzzz/Projects/python/apps/lib/python2.7/site-packages/django/core/management/__init__.py", line 367, in execute_from_command_line
utility.execute()
File "/Users/bubuzzz/Projects/python/apps/lib/python2.7/site-packages/django/core/management/__init__.py", line 359, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Users/bubuzzz/Projects/python/apps/lib/python2.7/site-packages/django/core/management/base.py", line 294, in run_from_argv
self.execute(*args, **cmd_options)
File "/Users/bubuzzz/Projects/python/apps/lib/python2.7/site-packages/django/core/management/base.py", line 345, in execute
output = self.handle(*args, **options)
File "/Users/bubuzzz/Projects/python/apps/lib/python2.7/site-packages/django/core/management/commands/makemigrations.py", line 173, in handle
migration_name=self.migration_name,
File "/Users/bubuzzz/Projects/python/apps/lib/python2.7/site-packages/django/db/migrations/autodetector.py", line 47, in changes
changes = self._detect_changes(convert_apps, graph)
File "/Users/bubuzzz/Projects/python/apps/lib/python2.7/site-packages/django/db/migrations/autodetector.py", line 132, in _detect_changes
self.old_apps = self.from_state.concrete_apps
File "/Users/bubuzzz/Projects/python/apps/lib/python2.7/site-packages/django/db/migrations/state.py", line 180, in concrete_apps
self.apps = StateApps(self.real_apps, self.models, ignore_swappable=True)
File "/Users/bubuzzz/Projects/python/apps/lib/python2.7/site-packages/django/db/migrations/state.py", line 242, in __init__
self.render_multiple(list(models.values()) + self.real_models)
File "/Users/bubuzzz/Projects/python/apps/lib/python2.7/site-packages/django/db/migrations/state.py", line 277, in render_multiple
model.render(self)
File "/Users/bubuzzz/Projects/python/apps/lib/python2.7/site-packages/django/db/migrations/state.py", line 559, in render
body,
File "/Users/bubuzzz/Projects/python/apps/lib/python2.7/site-packages/django/db/models/base.py", line 153, in __new__
raise TypeError("%s cannot proxy the swapped model '%s'." % (name, base_meta.swapped))
TypeError: Customer cannot proxy the swapped model 'core.MyUser'.
我不确定为什么customer
存在迁移脚本,因为在我的应用中,我曾经拥有Customer
模型,但我已经将其删除了。
然后,我创建了一个新的django项目来测试并尝试运行迁移。令人惊讶的是,我也看到了那些客户迁移步骤,但它运行成功。
Applying auth.0008_alter_user_username_max_length... OK
Applying auth.0009_customer... OK
Applying auth.0010_delete_customer... OK
Applying sessions.0001_initial... OK
简而言之,如何在Django 1.10中创建自定义用户?可以在此处查看示例代码https://github.com/bubuzzz/django-customer-swap-exception
答案 0 :(得分:1)
您不应删除迁移文件夹。如果你这样做,django不会为你做迁移。在核心应用程序中创建迁移文件夹,在其中创建一个空的__init__.py
文件,删除db.sqlite3文件,运行./manage.py makemigrations,然后迁移应该可以正常工作。
答案 1 :(得分:0)
Mehdi Pourfar的回答是正确的。如果您想了解更多详情
通过运行makemigrations,您告诉Django您已对模型进行了一些更改(在这种情况下,您已经创建了新模型),并且您希望将更改存储为迁移。 / p>
迁移是Django如何将更改存储到模型中(以及数据库模式) - 它们只是磁盘上的文件。如果您愿意,可以阅读新模型的迁移;这是文件polls / migrations / 0001_initial.py。不要担心,每次Django制作时都不会读它们,但如果你想手动调整Django改变的方式,它们的设计是人为可编辑的。
告诉django您要进行迁移的应用程序都可以解决您的问题。它会自动创建一个文件夹调用迁移保留您模型的记录。
python manage.py makemigrations core