Django 1.9.7 我正在使用pyenv virtualenv autoenv
我想扩展用户模型,所以我决定使用AbstractUser
(AbstractUser的class META
abstract = True,所以我不能创建表,但是继承类可以创建表,对吧?)
无论如何
(我们是项目名称)
我制作应用wef/users/models/__init__.py
from .user import User
在wef/users/models/user.py
from django.contrib.auth.models import AbstractUser
from django.db import models
class User(AbstractUser):
phonenumber = models.CharField(
max_length = 11,
blank = True,
null = True
)
我在settings.py
中添加了users
个应用
INSTALLED_APPS = [
[...]
'users',
]
AUTH_USER_MODEL = 'users.User'
所以,我认为当我制作素材时,请迁移
django将制作关于用户的模型表......
python wef/manage.py makemigrations users
显示错误
Traceback (most recent call last):
File "/Users/hanminsoo/.pyenv/versions/study_alone/lib/python3.5/site-packages/django/apps/config.py", line 163, in get_model
return self.models[model_name.lower()]
KeyError: 'user'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/Users/hanminsoo/.pyenv/versions/study_alone/lib/python3.5/site-packages/django/core/management/__init__.py", line 353, in execute_from_command_line
utility.execute()
File "/Users/hanminsoo/.pyenv/versions/study_alone/lib/python3.5/site-packages/django/core/management/__init__.py", line 345, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Users/hanminsoo/.pyenv/versions/study_alone/lib/python3.5/site-packages/django/core/management/base.py", line 348, in run_from_argv
self.execute(*args, **cmd_options)
File "/Users/hanminsoo/.pyenv/versions/study_alone/lib/python3.5/site-packages/django/core/management/base.py", line 398, in execute
self.check()
File "/Users/hanminsoo/.pyenv/versions/study_alone/lib/python3.5/site-packages/django/core/management/base.py", line 426, in check
include_deployment_checks=include_deployment_checks,
File "/Users/hanminsoo/.pyenv/versions/study_alone/lib/python3.5/site-packages/django/core/checks/registry.py", line 75, in run_checks
new_errors = check(app_configs=app_configs)
File "/Users/hanminsoo/.pyenv/versions/study_alone/lib/python3.5/site-packages/django/contrib/auth/checks.py", line 12, in check_user_model
cls = apps.get_model(settings.AUTH_USER_MODEL)
File "/Users/hanminsoo/.pyenv/versions/study_alone/lib/python3.5/site-packages/django/apps/registry.py", line 197, in get_model
return self.get_app_config(app_label).get_model(model_name.lower())
File "/Users/hanminsoo/.pyenv/versions/study_alone/lib/python3.5/site-packages/django/apps/config.py", line 166, in get_model
"App '%s' doesn't have a '%s' model." % (self.label, model_name))
LookupError: App 'users' doesn't have a 'user' model.
我无法理解为什么django找不到users.User模型
当我改变'AUTH_USER_MODEL = UserAAA'时
显示错误(大写字母改为小写)
LookupError: App 'users' doesn't have a 'useraaa' model.
我找不到我的问题 请有人帮助我..ㅠ_ㅠ
答案 0 :(得分:4)
我认为您已经创建了数据库架构。来自Django文档:
更改AUTH_USER_MODEL会对数据库结构产生很大影响。 它会更改可用的表,它会影响表 构建外键和多对多关系。如果你 打算设置AUTH_USER_MODEL,你应该在创建任何之前设置它 第一次迁移或运行manage.py迁移。
不支持在创建表后更改此设置 通过makemigrations,将导致您必须手动修复您的 schema,从旧用户表中移植数据,并可能手动移植 重新申请一些迁移。
答案 1 :(得分:0)
我遇到了同样的问题,我在删除此行后解决了这个问题
from .user import User
此外,删除所有用户&#39;创建自定义用户模型后在项目中导入