Opened 4 years ago

Last modified 4 years ago

#32692 closed Bug

Django 3.2 automatic AppConfig discovery breaks projects working in 3.1.8 — at Version 4

Reported by: Manel Clos Owned by: nobody
Component: contrib.admin Version: 3.2
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Manel Clos)

The described method of overriding the default admin site (https://docs.djangoproject.com/en/3.2/ref/contrib/admin/#overriding-the-default-admin-site), is not compatible with automatic AppConfig discovery, if the app (myproject, or myapps) is already in INSTALLED_APPS.

Example:

  • pip install Django==3.2
  • django-admin startproject myproject
  • python manage.py migrate
  • python manage.py createsuperuser
  • Put a template in myproject/templates/admin/base_site.html
    {% extends 'admin/base_site.html' %}
    
    {% block branding %}
    Template test
    {% endblock %}
    
  • Add myproject to INSTALLED_APPS
    INSTALLED_APPS = [
        'myproject',
    
        'django.contrib.admin',
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.messages',
        'django.contrib.staticfiles',
    ]
    
  • python manage.py runserver works, I can access the admin site and see the changes in templates
  • Follow the guide to override the default admin site, https://docs.djangoproject.com/en/3.2/ref/contrib/admin/#overriding-the-default-admin-site, this is, create myproject/admin.py, myproject/apps.py and modify settings.py.
    # myproject/admin.py
    from django.contrib import admin
    
    class MyAdminSite(admin.AdminSite):
        index_title = 'Test'
    
    
    # myproject/apps.py
    from django.contrib.admin.apps import AdminConfig
    
    class MyAdminConfig(AdminConfig):
        default_site = 'myproject.admin.MyAdminSite'
    
    
    # myproject/settings.py
    INSTALLED_APPS = [
        'myproject',
    
        'myproject.apps.MyAdminConfig',  # 'django.contrib.admin',
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.messages',
        'django.contrib.staticfiles',
    ]
    
  • python manage.py runserver

Actual result:
RuntimeError: 'myproject.apps' declares more than one default AppConfig: 'AdminConfig', 'MyAdminConfig'.`

Is this a regression?

  • pip install Django==3.1.8
  • python manage.py runserver
  • All ok, both templates and AdminConfig working at the same time

Workaroud

  • use app_admin.py instead of apps.py when following the documentation to override the default admin site
    # myproject/apps_admin.py
    from django.contrib.admin.apps import AdminConfig
    
    class MyAdminConfig(AdminConfig):
        default_site = 'myproject.admin.MyAdminSite'
    
  • adjust settings.py: `'myproject.apps_admin.MyAdminConfig' in INSTALLED_APPS
    INSTALLED_APPS = [
        'myproject',
    
        'myproject.apps_admin.MyAdminConfig',  # 'django.contrib.admin',
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.messages',
        'django.contrib.staticfiles',
    ]
    
  • works with Django 3.2!

Change History (4)

comment:1 by Manel Clos, 4 years ago

Description: modified (diff)

comment:2 by Mariusz Felisiak, 4 years ago

Component: Core (Other)contrib.admin
Description: modified (diff)
Resolution: duplicate
Status: newclosed

IMO it's a duplicate of #30402. You should customize a default admin site inside a main project directory (like described in the documentation) not in an app.

Please feel-free to send a PR with docs adjustments if you think we should make it clearer.

comment:3 by Manel Clos, 4 years ago

Description: modified (diff)
Resolution: duplicate
Status: closednew

Hi, I reviewed #30402 and it is not the same bug. Also, the steps to reproduce show that the admin site is customised inside the main project as you suggest. Reopening.

comment:4 by Manel Clos, 4 years ago

Description: modified (diff)

Description updated with the example code that was overwritten in comment 2

Note: See TracTickets for help on using tickets.
Back to Top