Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AUTH_USER_MODEL refers to model '%s' that has not been installed"

Tags:

python

django

I'm using a CustomUser in my model. This is the User Manager.

class UserManager(BaseUserManager):

    def create_user(self, email, username, password=None, is_staff=False, is_superuser=False, is_active=False,
                    is_bot=False, is_mobile_verified=False, is_online=True, is_logged_in=True):
        logger = logging.getLogger(__name__)
        logger.info("REGULAR user created!")
        if not email:
            raise ValueError('Email is required')
        if not username:
            raise ValueError('Username is required.')
        email = self.normalize_email(email)
        user = self.model(email=email, username=username, is_staff=is_staff, is_superuser=is_superuser,
                          is_active=is_active, is_bot=is_bot, is_mobile_verified=is_mobile_verified,
                          is_online=is_online, is_logged_in=is_logged_in)
        user.set_password(password)
        user.save(using=self._db)
        return user

    def create_superuser(self, email, username, password):
        logger = logging.getLogger(__name__)
        logger.info("SUPER user created!")
        return self.create_user(email, username, password=password, is_staff=True, is_superuser=True, is_active=True,
                                is_bot=False, is_mobile_verified=False, is_online=True, is_logged_in=True)

This is my definition of the custom user model.

class User(AbstractBaseUser, PermissionsMixin):
    email = models.EmailField(unique=True, max_length=255)
    mobile = PhoneNumberField(null=True)
    username = models.CharField(null=False, unique=True, max_length=255)
    full_name = models.CharField(max_length=255, blank=True, null=True)
    birthday = models.DateField(null=True)
    gender = models.CharField(max_length=255, null=True)
    is_staff = models.BooleanField(default=False)
    is_superuser = models.BooleanField(default=False)
    is_active = models.BooleanField(default=False)
    is_mobile_verified = models.BooleanField(default=False)
    is_online = models.BooleanField(default=False)
    is_logged_in = models.BooleanField(default=True)
    is_bot = models.BooleanField(default=False)
    location = models.ForeignKey(Location, on_delete=models.SET_NULL, null=True)

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = ['username']
    #objects = UserManager()

    get_user_model().objects.create_user(...)

If I uncomment the line objects = UserManager() then I can run the server but the super users created from the admin backend can't log in. If I use get_user_model() the code breaks and I get the following error

 "AUTH_USER_MODEL refers to model '%s' that has not been installed" % settings.AUTH_USER_MODEL
django.core.exceptions.ImproperlyConfigured: AUTH_USER_MODEL refers to model 'bouncer.User' that has not been installed

But in my settings.py I've define auth user model

AUTH_USER_MODEL = 'bouncer.User'

AUTHENTICATION_BACKENDS = (
    'django.contrib.auth.backends.ModelBackend',
)

What am I doing wrong here?

like image 575
Melissa Stewart Avatar asked Jul 15 '18 15:07

Melissa Stewart


2 Answers

For anyone reading this in 2020, I suspect the problem is dependency related. I ran into the same error as OP.

Your first two checks should be:

1 - Is the app in the installed apps list in your settings.py?

2 - Is the AUTH_USER_MODEL = "app_name_from_apps_py.model_name" set in settings.py?

This was as far as most of the other responses I read go.

What I didn't realise on reading the docs is that to use get_user_model() you need to have established your model first. Of course, right?!

So above, where OP is using get_user_model(), they are creating a circular dependency. You cannot use get_user_model() within the class that creates this model.

like image 69
Heartthrob_Rob Avatar answered Sep 19 '22 10:09

Heartthrob_Rob


That error looks like bouncer isn't in your INSTALLED_APPS.

So to clarify, you have to have

  • bouncer/models.py that contains the User model (or models/__init__.py which imports the model from another file)
  • 'bouncer' in the INSTALLED_APPS list in the settings
  • AUTH_USER_MODEL = 'bouncer.User' (as you do).
like image 35
AKX Avatar answered Sep 20 '22 10:09

AKX