Bula
Bula

Reputation: 2792

AUTH_USER_MODEL refers to model '' that has not been installed

I have a CustomUser that extends the AbstractUser as follows

class CustomUser(AbstractUser):

    USERTYPE_CHOICES = [(1,"A"),(2,"B"),(3,"C")]
    usertype = models.IntegerField(choices = USERTYPE_CHOICES, default = 1)

I have the following lines in my settings.py

INSTALLED_APPS = [
...
'login_register_service_hub.apps.LoginRegisterServiceHubConfig',
...
AUTH_USER_MODEL = 'login_register_service_hub.CustomUser'

Everything works as expected however:

I am trying to implement a custom authentication backend and I do this by including the following

from django.contrib.auth.backends import ModelBackend
from django.contrib.auth import get_user_model


#Check for the is_active property
class EmailBackend(ModelBackend):
    def authenticate(self, request, username=None, password=None, **kwargs):
        UserModel = get_user_model()
        try:
            user = UserModel.objects.get(email=username)
        except UserModel.DoesNotExist:
            return None
        else:
            if user.check_password(password):
                return user
        return None


    def get_user(self, user_id):
        UserModel = get_user_model()
        try:
            return UserModel.objects.get(pk=user_id)
        except UserModel.DoesNotExist:
            return None

I update my settings.py with the following:

AUTHENTICATION_BACKENDS = {'login_register_service_hub.EmailBackend',}

However as soon as I run

 python3 manage.py runserver

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 'login_register_service_hub.CustomUser' that has not been installed

The interesting remark is that the command fails only if I import ModelBackend (even if I comment out the EmailBackend definition)

Upvotes: 0

Views: 659

Answers (1)

Iain Shelvington
Iain Shelvington

Reputation: 32294

You should generally not import things in/from an apps __init__.py. Move your backend to login_register_service_hub/backends.py and then update your setting

AUTHENTICATION_BACKENDS = ['login_register_service_hub.backends.EmailBackend']

Upvotes: 2

Related Questions