我收到运行时错误Model类account.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',
]


account / apps.py

from django.apps import AppConfig


class AccountsConfig(AppConfig):
    name = 'accounts'

最佳答案

在您的User类声明上,包括:

class Meta:
    app_label = 'accounts'

关于python - Django在已安装的应用程序中没有明确的应用程序标签,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54679578/

10-11 17:43