I am getting an error
ImproperlyConfigured at /admin/
AUTH_USER_MODEL refers to model 'ledger.User' that has not been installed
I am only getting it on my production server. Not when I run things via localhost. First it was only when I was making a certain request. Then I thought my database must be out of sync so I deleted all the tables and then ran manage.py syncdb. Now, it seems to have propogated and even going to the admin throws the error.
I have never seen this error before and can't figure out what the deal is. I have defined the AUTH_USER_MODEL in settings.py:
...
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'ledger',
'extension',
'plugin',
'social.apps.django_app.default',
)
AUTH_USER_MODEL = 'ledger.User'
...
models.py:
...
class User(AbstractUser):
def __unicode__(self):
return self.username
balance = models.IntegerField(default=0)
total_pledged = models.IntegerField(default=0)
last_pledged = models.ForeignKey('Transaction', related_name='pledger', blank=True, null=True)
extension_key = models.CharField(max_length=100, null=True, blank=True)
plugin_key = models.CharField(max_length=100, null=True, blank=True)
ghosted = models.BooleanField(default=False)
def save(self, *args, **kwargs):
print('saving')
try:
self.company.save()
except:
print('no company')
super(User, self).save(*args, **kwargs)
...
I had this problem and it was solved by properly understanding how the Django filed are structured.
The instructions in tutorials are often different and confusing.
You need to understand that when you install Django, there are two key steps:
Let's illustrate the problem by following the official Django tutorial:
https://docs.djangoproject.com/en/3.1/intro/tutorial01/
Step 1: create a new project:
django-admin startproject mysite
You will now find there is a directory called "mysite"
**Step 2: ** the tutorial says: To create your app, make sure you’re in the same directory as manage.py and type this command: which is the directory just created, so go to:
cd mysite
and if you ls then you will find in this directory is: a file named manage.py confusingly, another directory named mysite
Step 3: Now the tutorial says to create your django application:
python manage.py startapp polls
So now ls shows these files:
manage.py
mysite
polls
Consufion has probably set in right now because all these files are under the mysite directory.
Step 4: If you want to use a custom user model, then the official advice is to do it right at the start of the project before doing any migrations.
OK, so edit mysite/settings.py and add the line:
AUTH_USER_MODEL = 'polls.User'
and edit polls/models and add:
from django.db import models
# Create your models here.
from django.contrib.auth.models import AbstractUser
class User(AbstractUser):
pass
** Step 5:** So now it should be OK to do the first migration, right?
python manage.py makemigrations
But SPLAT!
Traceback (most recent call last):
File "/opt/theapp/venv3.8/lib/python3.8/site-packages/django/apps/registry.py", line 156, in get_app_config
return self.app_configs[app_label]
KeyError: 'polls'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/opt/theapp/venv3.8/lib/python3.8/site-packages/django/contrib/auth/__init__.py", line 157, in get_user_model
return django_apps.get_model(settings.AUTH_USER_MODEL, require_ready=False)
File "/opt/theapp/venv3.8/lib/python3.8/site-packages/django/apps/registry.py", line 206, in get_model
app_config = self.get_app_config(app_label)
File "/opt/theapp/venv3.8/lib/python3.8/site-packages/django/apps/registry.py", line 163, in get_app_config
raise LookupError(message)
LookupError: No installed app with label 'polls'.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "manage.py", line 22, in <module>
main()
File "manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "/opt/theapp/venv3.8/lib/python3.8/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
utility.execute()
File "/opt/theapp/venv3.8/lib/python3.8/site-packages/django/core/management/__init__.py", line 377, in execute
django.setup()
File "/opt/theapp/venv3.8/lib/python3.8/site-packages/django/__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "/opt/theapp/venv3.8/lib/python3.8/site-packages/django/apps/registry.py", line 122, in populate
app_config.ready()
File "/opt/theapp/venv3.8/lib/python3.8/site-packages/django/contrib/admin/apps.py", line 24, in ready
self.module.autodiscover()
File "/opt/theapp/venv3.8/lib/python3.8/site-packages/django/contrib/admin/__init__.py", line 24, in autodiscover
autodiscover_modules('admin', register_to=site)
File "/opt/theapp/venv3.8/lib/python3.8/site-packages/django/utils/module_loading.py", line 47, in autodiscover_modules
import_module('%s.%s' % (app_config.name, module_to_search))
File "/usr/lib/python3.8/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
File "<frozen importlib._bootstrap>", line 991, in _find_and_load
File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 783, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/opt/theapp/venv3.8/lib/python3.8/site-packages/django/contrib/auth/admin.py", line 6, in <module>
from django.contrib.auth.forms import (
File "/opt/theapp/venv3.8/lib/python3.8/site-packages/django/contrib/auth/forms.py", line 21, in <module>
UserModel = get_user_model()
File "/opt/theapp/venv3.8/lib/python3.8/site-packages/django/contrib/auth/__init__.py", line 161, in get_user_model
raise ImproperlyConfigured(
django.core.exceptions.ImproperlyConfigured: AUTH_USER_MODEL refers to model 'polls.User' that has not been installed
(venv3.8) ubuntu@ip-172-26-5-79:~/mysite$
There is the error: .ImproperlyConfigured: AUTH_USER_MODEL refers to model 'polls.User' that has not been installed
So we fix this by modifying mysite/settings.py from this:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
to this:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'polls',
]
Notice that the line you needed to add was "polls" which is the name of your Django application.
Now try again:
(venv3.8) ubuntu@ip-172-26-5-79:~/mysite$ python manage.py makemigrations
Migrations for 'polls':
polls/migrations/0001_initial.py
- Create model User
(venv3.8) ubuntu@ip-172-26-5-79:~/mysite$
Success!!!
So the point of this long story is to make it clear that Django MUST know where your Django application is. And the place that you tell Django that, is in INSTALLED_APPS in the settings.py file.
It's really confusing the difference between Django project and Django app and its made worse by the strange suggestion to create two directories with the same name.
Instead, to make things much more clear I suggest you suffix your Django project name with "project" and suffix your Django application name with "app" and don't give the top level directory the same name as the project.
Thus to set up a new project:
(venv3.8) ubuntu@ip-172-26-5-79:~$ mkdir container
(venv3.8) ubuntu@ip-172-26-5-79:~$ cd container/
(venv3.8) ubuntu@ip-172-26-5-79:~/container$ django-admin startproject myproject .
(venv3.8) ubuntu@ip-172-26-5-79:~/container$ ls
manage.py myproject
(venv3.8) ubuntu@ip-172-26-5-79:~/container$ python manage.py startapp myapp
(venv3.8) ubuntu@ip-172-26-5-79:~/container$ ls -lah
total 20K
drwxrwxr-x 4 ubuntu ubuntu 4.0K Oct 27 05:30 .
drwxr-xr-x 11 ubuntu ubuntu 4.0K Oct 27 05:29 ..
-rwxrwx--- 1 ubuntu ubuntu 665 Oct 27 05:30 manage.py
drwxrwxr-x 3 ubuntu ubuntu 4.0K Oct 27 05:30 myapp
drwxrwxr-x 3 ubuntu ubuntu 4.0K Oct 27 05:30 myproject
(venv3.8) ubuntu@ip-172-26-5-79:~/container$
It's now going to be much more clear to you when you are dealing with the Django site and when you are dealing with the Django project and you will no longer be confused by there being multiple directories of the same name.
I'm aware this is an old question but I struggled with this issue for two days before finding my mistake, which was failing to follow the models organization in the Django Models docs.
If you have the AUTH_USER_MODEL = '<app_name>.<user_model>' (e.g. 'ledger.User') correctly written, and you have your '<app_name>', (e.g. ledger), in your INSTALLED_APPS list, but you're still getting this error, it's possible that your <custom_user> model (e.g. User) is in the wrong place.
It needs to be defined in either the models.py file in the top level of your app directory:
<app_name>/models.pyOR in a models/ directory containing an __init__.py file that imports your model:
<app_name>/models/<arbitrary_name>.py AND there is an <app_name>/models/__init__.py that contains the line from .<arbitrary_name> import <custom_user>If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With