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:
1: creating a project
2: creating an app (application)
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.