gpt4 book ai didi

python - 在 Django 中迁移自定义用户时出现异常

转载 作者:行者123 更新时间:2023-12-01 03:40:41 33 4
gpt4 key购买 nike

我需要为我的应用程序创建一个自定义用户,并完全遵循example来 self 的 settings.py 中的 AUTH_USER_MODEL = 'core.MyUser' 文档。但是,当我创建一个新数据库时,删除所有迁移文件夹并再次运行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

最佳答案

您不应删除迁移文件夹。如果你这样做,django 将不会为你进行迁移。在核心应用程序中创建迁移文件夹,在其中创建一个空的 __init__.py 文件,删除 db.sqlite3 文件,运行 ./manage.py makemigrations,然后迁移应该可以完美运行。

关于python - 在 Django 中迁移自定义用户时出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39682431/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com