Home > database >  AUTH_USER_MODEL reference in settings.py Django
AUTH_USER_MODEL reference in settings.py Django

Time:07-19

I have a installed an app called 'Login', who have a folder 'models' inside, with a custom_user model. The problem occurs when I tried to configure settings.py, specially auth_user_model.

in installed apps I have the following:

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'corsheaders',
'project_app.login'

]

and below

AUTH_USER_MODEL = 'login.models.CustomUser'

But I have the following error: "Invalid model reference. String model references must be of the form 'app_label.ModelName'." I put the .models in AUTH_USER_MODEL, because I want to reference the app that the CustomUser is inside the folder "models" in Login.

Also, I tried with declare like this:

AUTH_USER_MODEL = 'login.CustomUser'

but the error is this: 'AUTH_USER_MODEL refers to model 'login.CustomUser' that has not been installed'

CodePudding user response:

The issue is with the way that your app is installed - based on your AUTH_USER_MODEL, login should be the name of the app. It is acceptable to contain Django apps within folders for organisation purposes - however, your parent folder is project_app which from the name also appears to be an app. It's hard to say for certain what the issue is without knowing your project structure but I would expect changing your installed apps to just project_app and your AUTH_USER_MODEL to project_app.CustomerUser should work.

CodePudding user response:

If your app name is "login", you can proceed as follows:

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'corsheaders',

 #apps
'login.apps.LoginConfig']

I think the model in your login application is as follows:

class CustomUser(AbstractBaseUser):
    #some fields for customuser

and in settings.py:

AUTH_USER_MODEL = 'user.CustomUser'
AUTH_USER_MODEL = 'app_name.ModelName'
  • Related