django应用程序名称,但是安装了该应用程序
我正在尝试通过添加自定义用户模型来修改Django的内置验证系统。自定义模型是在一个名为 Accord
的应用程序中定义的:
from django.db import models
from django.contrib.auth.models import AbstractUser
from django.conf import settings
# Create your models here.
COUNTRIES = settings.COUNTRY_CHOICES
class CustomUser(AbstractUser):
zip_code = models.PositiveSmallIntegerField(blank=True, null=True)
city = models.CharField(blank=True, null=True, max_length=64)
address_street = models.CharField(blank=True, null=True, max_length=64)
address_number = models.CharField(blank=True, null=True, max_length=32)
country = models.CharField(blank=True, null=True, choices=COUNTRIES, max_length=8)
我以此方式更新了 settings.py
文件:
AUTH_USER_MODEL = accounts.CustomUser
我将 acception
作为已安装的应用程序:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'accounts',
'store',
'cart',
]
当我尝试运行 python manage.py makemigrations
命令以将更改应用于用户模型django丢弃此错误:
Traceback (most recent call last):
File "C:\Users\uhlar\Dev\ecommerce\manage.py", line 22, in <module>
main()
File "C:\Users\uhlar\Dev\ecommerce\manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "C:\Users\uhlar\.virtualenvs\ecommerce-vU6zMECh\lib\site-packages\django\core\management\__init__.py", line 446, in execute_from_command_line
utility.execute()
File "C:\Users\uhlar\.virtualenvs\ecommerce-vU6zMECh\lib\site-packages\django\core\management\__init__.py", line 386, in execute
settings.INSTALLED_APPS
File "C:\Users\uhlar\.virtualenvs\ecommerce-vU6zMECh\lib\site-packages\django\conf\__init__.py", line 87, in __getattr__
self._setup(name)
File "C:\Users\uhlar\.virtualenvs\ecommerce-vU6zMECh\lib\site-packages\django\conf\__init__.py", line 74, in _setup
self._wrapped = Settings(settings_module)
File "C:\Users\uhlar\.virtualenvs\ecommerce-vU6zMECh\lib\site-packages\django\conf\__init__.py", line 183, in __init__
mod = importlib.import_module(self.SETTINGS_MODULE)
File "C:\Users\uhlar\AppData\Local\Programs\Python\Python310\lib\importlib\__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 883, in exec_module
File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
File "C:\Users\uhlar\Dev\ecommerce\config\settings.py", line 149, in <module>
AUTH_USER_MODEL = accounts.CustomUser
NameError: name 'accounts' is not defined
该错误不仅限于 eaccect
应用程序,无论我在哪个应用程序中尝试定义 customuser
模型,我仍在获取它。
如果有人知道是什么原因引起了问题,请帮助我解决问题。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
/strong>设置&nbsp; sup> [django-doc] 应该成为 string ,而不是(合格的)名称,因此:
The
AUTH_USER_MODEL
setting [Django-doc] should be a string, not a (qualified) name, so:如Willem提到的,您需要在
auth_user_model
内置settings.py
account> accounts.customeruser 模型中更新auth_user_model
。此外,当您第一次运行迁移时,您将必须手动更新架构,Django将使用默认的auth.user
进行多个外键等。请参阅docs 此处用于更改用户中间项目。As Willem mentioned, you need to update your
AUTH_USER_MODEL
setting insidesettings.py
toaccounts.CustomerUser
model. In addition, you will have to update your schema manually as when you first run migrations, Django will use the defaultauth.User
for multiple foreign keys etc. See the docs here for changing a user mid project.