LookupError:没有已安装的应用标签'user'

时间:2015-09-14 17:30:57

标签: python django

我在运行以下代码时收到LookupError: No installed app with label 'user'

from django.db import models

from django.contrib.auth.models import AbstractBaseUser, BaseUserManager, PermissionsMixin

class UserManager(BaseUserManager):

    def _create_user(self, username, email, password, is_staff, is_superuser,
            **extra_fields):

        if not email:
            raise ValueError('El email debe de ser obligatorio')

        email = self.normalize_email(email)
        user = self.model(username=username, email=email, is_activate=True,
                is_staff=is_staff, is_superuser=is_superuser, **extra_fields)
        user.set_password(password)
        user.save(using = self.db)
        return user

    def create_user(self, username, email, password=None,
            **extra_fields):    
        return self._create_user(username, email, password, False, False, **extra_fields)

    def create_superuser(self, username, email, password,
            **extra_fields):
        return self._create_user(username, email, password, True, True, **extra_fields)

class User(AbstractBaseUser, PermissionsMixin):

    username = models.CharField(max_length=50, unique=True)
    email = models.EmailField(max_length=50, unique=True)
    first_name = models.CharField(max_length=100)
    last_name = models.CharField(max_length=100)
    avatar = models.URLField()


    objects = UserManager()

    is_activate = models.BooleanField(default=True)
    is_staff = models.BooleanField(default=False)

    USERNAME_FIELD = 'username'
    REQUIRE_FIELDS = ['email']

    def get_short_name(self):
        return self.username

错误的来源不是很明显,为什么会出现这个错误?

特别是,哪一行正在寻找'用户'标签,为什么它找不到它想要的东西?

2 个答案:

答案 0 :(得分:4)

您需要将settings.py中的AUTH_USER_MODEL设置为自定义用户模型

还包括在settings.py

中将自定义用户模型设为INSTALLED_APPS的应用

答案 1 :(得分:0)

在设置中验证:

AUTH_USER_MODEL = 'you_app_name.User'

并在已安装的应用中添加:

'your_app_name.apps.UsersConfig',