django应用程序名称,但是安装了该应用程序

发布于 2025-02-12 10:50:40 字数 3232 浏览 0 评论 0 原文

我正在尝试通过添加自定义用户模型来修改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 模型,我仍在获取它。

如果有人知道是什么原因引起了问题,请帮助我解决问题。

I'm trying to modify Django's built-in authetnication system by adding a custom user model. The customized model is defined inside an app named accounts:

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)

I updated the settings.py file this way:

AUTH_USER_MODEL = accounts.CustomUser

I added accounts as an installed app:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'accounts',
    'store',
    'cart',
]

When I try to run the python manage.py makemigrations command in terminal to apply changes made to the user model Django's throwing this error:

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

The error is not only confined to the accounts app, no matter inside which app I try to define the CustomUser model I'm still getting it.

If anyone knows what's causing the problem please help me to resolve it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

九命猫 2025-02-19 10:50:40

/strong>设置&nbsp; sup> [django-doc] 应该成为 string ,而不是(合格的)名称,因此:

# settings.py

# …

# string literal

The AUTH_USER_MODEL setting [Django-doc] should be a string, not a (qualified) name, so:

# settings.py

# …

#  string literal 🖟                   🖟
AUTH_USER_MODEL = 'accounts.CustomUser'
亽野灬性zι浪 2025-02-19 10:50:40

如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 inside settings.py to accounts.CustomerUser model. In addition, you will have to update your schema manually as when you first run migrations, Django will use the default auth.User for multiple foreign keys etc. See the docs here for changing a user mid project.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文