NeuroAgent

Why Django Can't Find Admin App During Migration

Solution for KeyError: 'admin' error in Django when migrating custom user. Learn the correct INSTALLED_APPS and URL configuration setup.

Why can’t Django find my ‘admin’ app when running migrations?

I created a custom user in my Django application, but when I try to run migrations, I get an error:

Traceback (most recent call last):
  File "C:\pch_projects\net-base\microservices\user-service\user-env\Lib\site-packages\django\apps\registry.py", line 158, in get_app_config
    return self.app_configs[app_label]
           ~~~~~~~~~~~~~~~~^^^^^^^^^^^
KeyError: 'admin'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\pch_projects\net-base\microservices\user-service\manage.py", line 22, in <module>
    main()
    ~~~~^^
  File "C:\pch_projects\net-base\microservices\user-service\manage.py", line 18, in main
    execute_from_command_line(sys.argv)
    ~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^
  File "C:\pch_projects\net_base\microservices\user-service\user-env\Lib\site-packages\django\core\management\__init__.py", line 442, in execute_from_command_line
    utility.execute()
    ~~~~~~~~~~~~~~~^^
  File "C:\pch_projects\net-base\microservices\user-service\user-env\Lib\site-packages\django\core\management\__init__.py", line 436, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^
  File "C:\pch_projects\net-base\microservices\user-service\user-env\Lib\site-packages\django\core\management\base.py", line 416, in run_from_argv
    self.execute(*args, **cmd_options)
    ~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^
  File "C:\pch_projects\net-base\microservices\user-service\user-env\Lib\site-packages\django\core\management\base.py", line 457, in execute
    self.check(**check_kwargs)
    ~~~~~~~~~~^^^^^^^^^^^^^^^^
  File "C:\pch_projects\net-base\microservices\user-service\user-env\Lib\site-packages\django\core\management\base.py", line 492, in check
    all_issues = checks.run_checks(
        app_configs=app_configs,
    ...<2 lines>...
        databases=databases,
    )
  File "C:\pch_projects\net-base\microservices\user-service\user-env\Lib\site-packages\django\core\checks\registry.py", line 89, in run_checks
    new_errors = check(app_configs=app_configs, databases=databases)
  File "C:\pch_projects\net-base\microservices\user-service\user-env\Lib\site-packages\django\core\checks\urls.py", line 44, in check_url_namespaces_unique
    all_namespaces = _load_all_namespaces(resolver)
  File "C:\pch_projects\net-base\microservices\user-service\user-env\Lib\site-packages\django\core\checks\urls.py", line 63, in _load_all_namespaces
    url_patterns = getattr(resolver, "url_patterns", [])
  File "C:\pch_projects\net-base\microservices\user-service\user-env\Lib\site-packages\django\utils\functional.py", line 47, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
                                         ~~~~~~~~~^^^^^^^^^^
  File "C:\pch_projects\net-base\microservices\user-service\user-env\Lib\site-packages\django\urls\resolvers.py", line 718, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
                       ^^^^^^^^^^^^^^^^^^^
  File "C:\pch_projects\net-base\microservices\user-service\user-env\Lib\site-packages\django\utils\functional.py", line 47, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
                                         ~~~~~~~~~^^^^^^^^^^
  File "C:\pch_projects\net-base\microservices\user-service\user-env\Lib\site-packages\django\urls\resolvers.py", line 711, in urlconf_module
    return import_module(self.urlconf_name)
  File "C:\Users\miron\AppData\Local\Programs\Python\Python313\Lib\importlib\__init__.py", line 88, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib._bootstrap>", line 1387, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1331, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 935, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 1026, in exec_module
  File "<frozen importlib._bootstrap_external>", line 488, in _call_with_frames_removed
  File "C:\pch_projects\net-base\microservices\user-service\config\urls.py", line 21, in <module>
    path('admin/', admin.site.urls),
                   ^^^^^^^^^^^^^^^
  File "C:\pch_projects\net-base\microservices\user-service\user-env\Lib\site-packages\django\utils\functional.py", line 251, in inner
    self._setup()
    ~~~~~~~~~~~^^
  File "C:\pch_projects\net-base\microservices\user-service\user-env\Lib\site-packages\django\contrib\admin\sites.py", line 610, in _setup
    AdminSiteClass = import_string(apps.get_app_config("admin").default_site)
                                   ~~~~~~~~~~~~~~~~~~~^^^^^^^^^
  File "C:\pch_projects\net-base\microservices\user-service\user-env\Lib\site-packages\django\apps\registry.py", line 165, in get_app_config
    raise LookupError(message)
LookupError: No installed app with label 'admin'.

I tried commenting out the admin path, but now Django can’t see my ‘user’ app. Here’s my INSTALLED_APPS configuration:

python
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'rest_framework',
    'user.apps.UserConfig',
]

How do I properly configure the app so Django can find and use it?

NeuroAgent

The “No installed app with label ‘admin’” error in Django occurs when the system cannot find the built-in admin app in the INSTALLED_APPS list or during its configuration. This is a common problem when working with custom users and incorrect application configuration.

Contents


Basic causes of the error

The error occurs due to several common problems:

  1. Admin import issue - Django cannot import the admin module due to incorrect configuration
  2. Incorrect URL configuration - the error occurs when checking URL paths before migrations are executed
  3. Conflict with custom app - if your app is named ‘admin’ or has conflicting settings
  4. INSTALLED_APPS order issues - the order of apps in the list can affect their detection

Checking INSTALLED_APPS configuration

In your case, the INSTALLED_APPS configuration looks correct, but there’s an important point:

python
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'rest_framework',
    'user.apps.UserConfig',  # Correct configuration specification
]

The problem is not in the INSTALLED_APPS list itself, but that the error occurs at the URL configuration stage, before migrations should even be executed.

Custom user setup

When working with custom users, it’s important to properly configure the user model. The error can occur if:

  1. You created a custom user but didn’t configure it properly
  2. You missed the step of registering the custom user in AUTH_USER_MODEL
  3. There are issues with custom model migrations

Make sure you have in your settings.py:

python
AUTH_USER_MODEL = 'user.User'  # or your app name and model name

Proper URL configuration

The error occurs in the config/urls.py file at the line:

python
path('admin/', admin.site.urls),

The problem is that Django tries to access admin.site.urls, but at this stage the admin app is not fully initialized yet. This can happen due to import order or configuration issues.

Problem solution

To solve this problem, follow these steps:

1. Check import order in urls.py

Make sure that in your config/urls.py, all necessary modules are imported first:

python
from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    # other paths
]

2. Update INSTALLED_APPS configuration

If you have an app named ‘admin’, rename it as it conflicts with the built-in admin app.

3. Check your app configuration

In the user/apps.py file, there should be the correct configuration:

python
from django.apps import AppConfig

class UserConfig(AppConfig):
    default_auto_field = 'django.db.models.BigAutoField'
    name = 'user'

    def ready(self):
        # possibly something needs to be configured here
        pass

4. Temporarily comment out admin for migrations

To run migrations, you can temporarily comment out admin in urls.py:

python
# from django.contrib import admin
# from django.urls import path, include

# urlpatterns = [
#     # path('admin/', admin.site.urls),
#     # other paths
# ]

After running migrations, uncomment it back.

Example of proper setup

Here is the complete correct setup for a custom user:

models.py

python
from django.contrib.auth.models import AbstractUser
from django.db import models

class User(AbstractUser):
    # your additional fields
    phone = models.CharField(max_length=20, blank=True, null=True)

apps.py

python
from django.apps import AppConfig

class UserConfig(AppConfig):
    default_auto_field = 'django.db.models.BigAutoField'
    name = 'user'

settings.py

python
AUTH_USER_MODEL = 'user.User'

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'rest_framework',
    'user.apps.UserConfig',
]

urls.py (at the root of the project)

python
from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('api/', include('user.urls')),  # if you have API
]

Sources

  1. Official Django Documentation - Custom User Model
  2. Django Admin Documentation
  3. Django INSTALLED_APPS Documentation
  4. Stack Overflow - Django admin migration error

Conclusion

The main cause of your error is a conflict during admin initialization before running migrations. To solve the problem:

  1. Make sure your app is correctly specified as ‘user.apps.UserConfig’ in INSTALLED_APPS
  2. Check that there are no conflicting app names
  3. Temporarily comment out admin in urls.py to run migrations
  4. After migrations, uncomment admin back
  5. Make sure AUTH_USER_MODEL correctly points to your custom user model

By following these steps, you can successfully run migrations and set up the admin panel for your custom user in Django.