[Django] Solution for 'Error: AUTH_USER_MODEL refers to model 'users.User' that has not been installed'

2023. 9. 21. 20:11Django

Token 만들기를 하는 중이다.

아래 사이트에서 Customizing authentication 을 복사해서 붙여넣었다.

https://docs.djangoproject.com/en/4.2/topics/auth/customizing/

 

Django

The web framework for perfectionists with deadlines.

docs.djangoproject.com

복사하여 Myuser 부분만 약간 수정함.

#models.py

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


class UserManager(BaseUserManager):
    def create_user(self, email, password=None):
        """
        Creates and saves a User with the given email, date of
        birth and password.
        """
        if not email:
            raise ValueError("Users must have an email address")

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

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

    def create_superuser(self, email, password=None):
        """
        Creates and saves a superuser with the given email, date of
        birth and password.
        """
        user = self.create_user(
            email,
            password=password,
        )
        user.is_admin = True
        user.save(using=self._db)
        return user


class User(AbstractBaseUser):
    email = models.EmailField(
        verbose_name="email address",
        max_length=255,
        unique=True,
    )
    is_active = models.BooleanField(default=True)
    is_admin = models.BooleanField(default=False)

    objects = UserManager()

    USERNAME_FIELD = "email"

    def __str__(self):
        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?"
        # Simplest possible answer: All admins are staff
        return self.is_admin

그리고 settings.py에는 아래와 같이 추가했다.

#settings.py

AUTH_USER_MODEL = 'users.User'

이전의 DB를 지우고 이제 migration을 하려고 명령했더니,

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

에러 메시지 발견.

 

이리찾고 저리 찾다가, install? settings 의 INSTALLED.APPS 말하는건가? 싶어서 가봤더니 역시나 맞았던...

users 추가하니까 바로 해결되었다.

햅삐한 결론🐥💛🍋