【问题标题】:"AUTH_USER_MODEL refers to model '%s' that has not been installed" % settings.AUTH_USER_MODEL django.core.exceptions.ImproperlyConfigured:“AUTH_USER_MODEL 指的是尚未安装的模型 '%s'” % settings.AUTH_USER_MODEL django.core.exceptions.ImproperlyConfigured:
【发布时间】:2020-07-22 09:55:59
【问题描述】:

我正在学习 django restframework,当我在我的 settings.py 中尝试此 AUTH_USER_MODEL = 'homepage.Account' 时,我收到此错误 "AUTH_USER_MODEL 指的是模型 '%s'尚未安装” % settings.AUTH_USER_MODEL django.core.exceptions.ImproperlyConfigured:AUTH_USER_MODEL 指的是尚未安装的模型“homepage.Account”我无法进行迁移。

这是我的 settings.py

INSTALLED_APPS = [
    'homepage',
    'django_tables2',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.humanize',
    'rest_framework',
    'rest_framework.authtoken',
]
REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': [
        'rest_framework.authentication.TokenAuthentication',

    ]
}

AUTH_USER_MODEL = 'homepage.Account'

这是我的文件树

我的文件

myproject
     |_homepage
       |_ __init__.py
       |_admin.py
       |_models.py
       |_apps.py
       |_views.py
     |_schoolsite
       |_ __init__.py
       |_settings.y
       |_urls.py
       |_wsgi.py

这是我的models.py

class MyAccountManager(BaseUserManager):
    def create_user(self, email, username, password=None):
        if not email:
            raise ValueError('Users must have an email address')
        if not username:
            raise ValueError('Users must have a username')

        user = self.model(
            email=self.normalize_email(email),
            username=username,
        )

        user.set_password(password)
        user.save(using=self._db)
        return user

    def create_superuser(self, email, username, password):
        user = self.create_user(
            email=self.normalize_email(email),
            password=password,
            username=username,
        )
        user.is_admin = True
        user.is_staff = True
        user.is_superuser = True
        user.save(using=self._db)
        return user


class Account(AbstractBaseUser):
    email                   = models.EmailField(verbose_name="email", max_length=60, unique=True)
    username                = models.CharField(max_length=30, unique=True)
    date_joined             = models.DateTimeField(verbose_name='date joined', auto_now_add=True)
    last_login              = models.DateTimeField(verbose_name='last login', auto_now=True)
    is_admin                = models.BooleanField(default=False)
    is_active               = models.BooleanField(default=True)
    is_staff                = models.BooleanField(default=False)
    is_superuser            = models.BooleanField(default=False)


    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = ['username']

    objects = MyAccountManager()

    def __str__(self):
        return self.email

    # For checking permissions. to keep it simple all admin have ALL permissons
    def has_perm(self, perm, obj=None):
        return self.is_admin

    # Does this user have permission to view this app? (ALWAYS YES FOR SIMPLICITY)
    def has_module_perms(self, app_label):
        return True
   
@receiver(post_save, sender=settings.AUTH_USER_MODEL)
def create_auth_token(sender, instance=None, created=False, **kwargs):
    if created:
        Token.objects.create(user=instance)

class myaccount(models.Model):
  client_name = models.CharField(max_length=500, null=True,blank=True)
  product_name = models.CharField(max_length=500, null=True,blank=True)
  amount = models.FloatField(max_length=500, null=True,blank=True)
  price = models.FloatField(max_length=500, null=True,blank=True)
  comment = models.CharField(max_length=500, null=True,blank=True)
  created = models.DateTimeField(auto_now_add=True)
  def __str__(self):
        suser = '{0.client_name} {0.product_name}'
        return suser.format(self)

【问题讨论】:

    标签: python django


    【解决方案1】:

    也许先尝试运行迁移,然后在您的设置中设置 AUTH_USER_MODEL。

    【讨论】:

    • 尝试在 INSTALLED_APPS 末尾添加“主页”。 INSTALLED_APPS = [ .... '主页', ]
    • 将其添加到 INSTALLED_APPS 的末尾,而不是开头。
    • 是的,因为当应用程序运行时,开始按顺序查找 INSTALLED_APPS。所以,如果它在这个应用程序中没有找到“django.contrib.auth”,那么如果我们使用的是 AUTH_USER_MODEL,它就会出错。为避免这种情况,最佳做法是将我们的应用名称添加到 INSTALLED_APPS 的末尾。
    猜你喜欢
    • 1970-01-01
    • 2019-09-30
    • 1970-01-01
    • 2022-08-15
    • 1970-01-01
    • 2015-01-10
    • 1970-01-01
    • 1970-01-01
    • 2021-10-07
    相关资源
    最近更新 更多