第 158 行,在 get_app_config 中返回 self.app_configs[app_label] KeyError: 'account'

问题描述 投票:0回答:1

我正在开发一个django项目,主目录/项目名称是

django-basic-ecommerce

其中一个应用程序名称是

accounts
(请注意,它的最后一个“s”)。

当我奔跑时

python manage.py makemigrations

我明白了

Traceback (most recent call last):    
  File "/my/path/basic-django-ecommerce/venv/lib/python3.10/site-packages/django/apps/registry.py", line 158, in get_app_config    
    return self.app_configs[app_label]    
KeyError: 'account'

During handling of the above exception, another exception occurred:

[...]
/my/path/basic-django-ecommerce/venv/lib/python3.10/site-packages/django/apps/registry.py", line 165, in get_app_config
    raise LookupError(message)
LookupError: No installed app with label 'account'.

During handling of the above exception, another exception occurred:

[...]   
/my/path/basic-django-ecommerce/venv/lib/python3.10/site-packages/django/contrib/auth/__init__.py", line 176, in get_user_model
raise ImproperlyConfigured(django.core.exceptions.ImproperlyConfigured: AUTH_USER_MODEL refers to model 'account.User' that has not been installed

最后一条消息让我觉得我错过了

account.User
的一些配置,但是,它已正确安装在
mainapp_ecommerce/settings.py

INSTALLED_APPS = [
...
'accounts',
...
'django.contrib.auth',
...
]

AUTH_USER_MODEL = 'account.User' 
# it changes the builtin model user to User custom defined model

关于

AUTH_USER_MODEL
,它指的是帐户。在
accounts/models.py
中定义的用户模型为

class User(AbstractBaseUser):
    email = models.EmailField(max_length=255, unique=True)
    # full_name = models.CharField(max_length=255, blank=True, null=True)
    active = models.BooleanField(default=True)  # is h* allowed to login?
    staff = models.BooleanField(default=False) # staff user, not superuser
    admin = models.BooleanField(default=False) # superuser
    timestamp = models.DateTimeField(auto_now_add=True) # superuser

    USERNAME_FIELD = 'username'

    # email and password are required

    REQUIRED_FIELDS = []

    objects = UserManager

那么,这里可能存在什么问题?

python django django-models django-migrations
1个回答
0
投票

事实证明

AUTH_USER_MODEL refers to model 'account.User' that has not been installed

因为我没有跑

python manage.py makemigrations

就在我定义模型用户之后。

所以,为了解决,我在settings.py中注释了

AUTH_USER_MODEL = 'account.User'
,运行
python manage.py makemigrations
,当django询问时手动插入默认值
timezone.now

It is impossible to add the field 'timestamp' with 'auto_now_add=True' to user without providing a default. This is because the database needs something to populate existing rows.

 1) Provide a one-off default now which will be set on all existing rows

 2) Quit and manually define a default value in models.py.

Select an option: 1

Please enter the default value as valid Python.

Accept the default 'timezone.now' by pressing 'Enter' or provide another value.

The datetime and django.utils.timezone modules are available, so it is possible to provide e.g. timezone.now as a value.

Type 'exit' to exit this prompt

[default: timezone.now] >>> timezone.now

这样迁移就顺利了

Migrations for 'accounts':
  accounts/migrations/0004_user_timestamp_profile.py
    - Add field timestamp to user
    - Create model Profile

最后我取消注释

AUTH_USER_MODEL = 'account.User'
并运行

python manage.py migrate
© www.soinside.com 2019 - 2024. All rights reserved.