[英]Django Admin: Custom User model in app section instead of auth section
As in question 36046698 , I have a CustomUser model defined in models.py
.如问题 36046698 一样,我在
models.py
中定义了一个 CustomUser model 。
I would like it to appear in the Authentication & Authorization
section in Django Admin.我希望它出现在 Django Admin 的
Authentication & Authorization
部分。
I tried adding app_label = 'auth' as suggested, ie我尝试按照建议添加 app_label = 'auth' ,即
class CustomUser(AbstractUser):
class Meta:
app_label = 'auth'
When I do that, my app won't start, and errors out like so:当我这样做时,我的应用程序将无法启动,并且错误如下:
Traceback (most recent call last):
File "/Users/paul/myapp/.direnv/python-3.8.5/lib/python3.8/site-packages/django/apps/config.py", line 178, in get_model
return self.models[model_name.lower()]
KeyError: 'customuser'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/paul/myapp/.direnv/python-3.8.5/lib/python3.8/site-packages/django/contrib/auth/__init__.py", line 157, in get_user_model
return django_apps.get_model(settings.AUTH_USER_MODEL, require_ready=False)
File "/Users/paul/myapp/.direnv/python-3.8.5/lib/python3.8/site-packages/django/apps/registry.py", line 211, in get_model
return app_config.get_model(model_name, require_ready=require_ready)
File "/Users/paul/myapp/.direnv/python-3.8.5/lib/python3.8/site-packages/django/apps/config.py", line 180, in get_model
raise LookupError(
LookupError: App 'myapp' doesn't have a 'CustomUser' model.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "manage.py", line 21, in <module>
main()
File "manage.py", line 17, in main
execute_from_command_line(sys.argv)
File "/Users/paul/myapp/.direnv/python-3.8.5/lib/python3.8/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
....
File "/Users/paul/myapp/myapp/admin.py", line 3, in <module>
from django.contrib.auth.admin import UserAdmin
File "/Users/paul/myapp/.direnv/python-3.8.5/lib/python3.8/site-packages/django/contrib/auth/admin.py", line 6, in <module>
from django.contrib.auth.forms import (
File "/Users/paul/myapp/.direnv/python-3.8.5/lib/python3.8/site-packages/django/contrib/auth/forms.py", line 21, in <module>
UserModel = get_user_model()
File "/Users/paul/myapp/.direnv/python-3.8.5/lib/python3.8/site-packages/django/contrib/auth/__init__.py", line 161, in get_user_model
raise ImproperlyConfigured(
django.core.exceptions.ImproperlyConfigured: AUTH_USER_MODEL refers to model 'myapp.CustomUser' that has not been installed
In settings.py
, I have在
settings.py
,我有
AUTH_USER_MODEL = 'myapp.CustomUser'
If I comment out that line, the server starts, but page loads fail with:如果我注释掉该行,服务器将启动,但页面加载失败:
ValueError: Cannot create form field for 'user' yet, because its related model
'CustomUser' has not been loaded yet
This seems to be the same crash as question 62394664 .这似乎与问题 62394664相同的崩溃。
It's seems like you forgot to migrate database.好像您忘记迁移数据库了。 try with this command
python manage.py makemigrations python manage.py migrate
试试这个命令
python manage.py makemigrations python manage.py migrate
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.