Home > front end >  django.core.exceptions.ImproperlyConfigured: AUTH_USER_MODEL refers to model 'app.User' th
django.core.exceptions.ImproperlyConfigured: AUTH_USER_MODEL refers to model 'app.User' th

Time:11-11

this question has been asked a lot of times but I am asking it because I still cannot find any workable solution. I am trying to create a custom user model using AbstractBaseUser inside the myapp app and I have written the app name inside INSTALLED_APPS and the command AUTH_USER_MODEL = 'myapp.User' inside settings.py. The command python manage.py makemigrations --dry-run works perfectly without any errors when my custom user model is stored within the default model file location at backend/myapp/models.py but I want to rename and move this file to the directory backend/myapp/models/user.py and have it stored along with other models. Whenever I try and move or rename the backend/myapp/models.py file which has my custom user model, Django returns the django.core.exceptions.ImproperlyConfigured error. Need help. Thanks

backend/myapp/models.py:

from django.db import models
from django.contrib.auth.models import AbstractBaseUser

class User(AbstractBaseUser):
    id = models.CharField(max_length=15, null=False, blank=False, primary_key=True, unique=True)

    USERNAME_FIELD = "id"

Current folder structure:

[backend]/
├── [core]/
│   ├── __pycache__/
│   ├── __init__.py
│   ├── asgi.py
│   ├── settings.py
│   ├── urls.py
│   ├── wsgi.py
├── [env]/
├── [myapp]/
│   ├── __pycache__/
│   ├── migrations/
│   ├── __init__.py
│   ├── admin.py
│   ├── apps.py
│   ├── models.py
├── manage.py

django.core.exceptions.ImproperlyConfigured:

C:\Users\amand\Documents\_My Files\project\app\backend>python manage.py makemigrations --dry-run
Traceback (most recent call last):
  File "C:\Users\amand\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\django\apps\config.py", line 268, in get_model
    return self.models[model_name.lower()]
KeyError: 'user'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\amand\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\django\contrib\auth\__init__.py", line 160, in get_user_model
    return django_apps.get_model(settings.AUTH_USER_MODEL, require_ready=False)
  File "C:\Users\amand\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\django\apps\registry.py", line 211, in get_model
    return app_config.get_model(model_name, require_ready=require_ready)
  File "C:\Users\amand\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\django\apps\config.py", line 270, in get_model
    raise LookupError(
LookupError: App 'myapp' doesn't have a 'User' model.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\amand\Documents\_My Files\project\app\backend\manage.py", line 22, in <module>
    main()
  File "C:\Users\amand\Documents\_My Files\project\app\backend\manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "C:\Users\amand\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\django\core\management\__init__.py", line 419, in execute_from_command_line
    utility.execute()
  File "C:\Users\amand\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\django\core\management\__init__.py", line 395, in execute
    django.setup()
  File "C:\Users\amand\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\django\__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "C:\Users\amand\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\django\apps\registry.py", line 122, in populate
    app_config.ready()
  File "C:\Users\amand\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\django\contrib\admin\apps.py", line 27, in ready
    self.module.autodiscover()
  File "C:\Users\amand\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\django\contrib\admin\__init__.py", line 24, in autodiscover
    autodiscover_modules('admin', register_to=site)
  File "C:\Users\amand\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\django\utils\module_loading.py", line 47, in autodiscover_modules
    import_module('%s.%s' % (app_config.name, module_to_search))
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.2288.0_x64__qbz5n2kfra8p0\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 850, in exec_module
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  File "C:\Users\amand\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\django\contrib\auth\admin.py", line 6, in <module>
    from django.contrib.auth.forms import (
  File "C:\Users\amand\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\django\contrib\auth\forms.py", line 21, in <module>
    UserModel = get_user_model()
  File "C:\Users\amand\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\django\contrib\auth\__init__.py", line 164, in get_user_model
    raise ImproperlyConfigured(
django.core.exceptions.ImproperlyConfigured: AUTH_USER_MODEL refers to model 'myapp.User' that has not been installed    

C:\Users\amand\Documents\_My Files\project\app\backend>

CodePudding user response:

In settings.py Add myapp in INSTALLED_APPS

INSTALLED_APPS = [
    ...,
    'myapp',
]

I User model add app_label = 'myapp' like

class User(AbstractBaseUser):
    id = models.CharField(max_length=15, null=False, blank=False, primary_key=True, unique=True)

    USERNAME_FIELD = "id"

    class Meta:
        app_label = 'myapp' 

If you want to move the model in a separate file. The directory structure should like

myapp
   models
       __init__.py
       user.py

Put User model code in user.py Then import User model in __init__.py

from myapp.models.user import User
__all__ = ["User"]
  • Related