Django 在 INSTALLED APPS 中没有明确的应用标签

Posted

技术标签:

【中文标题】Django 在 INSTALLED APPS 中没有明确的应用标签【英文标题】:Django no explicit app label in INSTALLED APPS 【发布时间】:2019-07-07 20:10:56 【问题描述】:

我收到运行时错误模型类 accounts.models.User 没有声明明确的 app_label 并且不在 INSTALLED_APPS 中的应用程序中。花了几个小时在网上搜索解决方案后,我仍然无法弄清楚我做错了什么。 (注意:使用 Django 1.11)

models.py

class User(AbstractBaseUser):
    email = models.EmailField(
        verbose_name='email address',
        max_length=255,
        unique=True,
    )
    active = models.BooleanField(default=True)
    staff = models.BooleanField(default=False) # a admin user; non super-user
    admin = models.BooleanField(default=False) # a superuser
    verified = models.BooleanField(default=False)

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = [] # Email & Password are required by default.

    objects = UserManager()

    def get_full_name(self):
        # The user is identified by their email address
        return self.email

    def get_short_name(self):
        # The user is identified by their email address
        return self.email

    def __str__(self):              # __unicode__ on Python 2
        return self.email

    def has_perm(self, perm, obj=None):
        "Does the user have a specific permission?"
        # Simplest possible answer: Yes, always
        return True

    def has_module_perms(self, app_label):
        "Does the user have permissions to view the app `app_label`?"
        # Simplest possible answer: Yes, always
        return True

    @property
    def is_staff(self):
        "Is the user a member of staff?"
        return self.staff

    @property
    def is_admin(self):
        "Is the user a admin member?"
        return self.admin

    @property
    def is_active(self):
        "Is the user active?"
        return self.active

    @property
    def is_verified(self):
        "Has the user verified their email?"
        return self.verified

    def email_user(self, subject, message, from_email=None, **kwargs):
        send_mail(subject, message, from_email, [self.email], **kwargs)

settings.py

AUTH_USER_MODEL = 'accounts.User'

...

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'rest_framework',
    'rest_framework.authtoken',
    'accounts',
    'demoapp',
]

accounts/apps.py

from django.apps import AppConfig


class AccountsConfig(AppConfig):
    name = 'accounts'

【问题讨论】:

尝试将AUTH_USER_MODEL 声明放在INSTALLED_APPS 声明之后。 【参考方案1】:

在您的 User 类声明中包括:

class Meta:
    app_label = 'accounts'

【讨论】:

另一个想法。如果您从 AbstractUser 而不是 AbstractBaseUser 继承,则设置自定义用户模型要容易得多:docs.djangoproject.com/en/2.1/topics/auth/customizing/… 和这个:docs.djangoproject.com/en/2.1/topics/auth/customizing/…

以上是关于Django 在 INSTALLED APPS 中没有明确的应用标签的主要内容,如果未能解决你的问题,请参考以下文章

如何解决 Django 中的“请求设置 INSTALLED_APPS,但未配置设置。”错误?

django.core.exceptions.ImproperlyConfigured:请求设置 INSTALLED_APPS,但未配置设置。请帮帮我

django---加载INSTALLED_APPS的源码分析

Django1.10 错误:django.contrib.contenttypes.models.ContentType 未声明显式 app_label 且不在 INSTALLED_APPS 中的应用

ImproperlyConfigured:请求设置 INSTALLED_APPS,但未配置设置 - Scraper

模型类 django.contrib.sessions.models.Session 没有声明明确的 app_label 并且不在 INSTALLED_APPS 中的应用程序中