Changes between Version 3 and Version 4 of Ticket #32692


Ignore:
Timestamp:
Apr 28, 2021, 7:03:03 AM (4 years ago)
Author:
Manel Clos
Comment:

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

Legend:

Unmodified
Added
Removed
Modified
  • Ticket #32692 – Description

    v3 v4  
    11The 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.
    22
    3 **How to reproduce**
     3Example:
    44- `pip install Django==3.2`
    55- `django-admin startproject myproject`
     
    77- `python manage.py createsuperuser`
    88- Put a template in `myproject/templates/admin/base_site.html`
     9{{{
     10{% extends 'admin/base_site.html' %}
     11
     12{% block branding %}
     13Template test
     14{% endblock %}
     15}}}
    916- Add `myproject` to `INSTALLED_APPS`
     17{{{#!python
     18INSTALLED_APPS = [
     19    'myproject',
     20
     21    'django.contrib.admin',
     22    'django.contrib.auth',
     23    'django.contrib.contenttypes',
     24    'django.contrib.sessions',
     25    'django.contrib.messages',
     26    'django.contrib.staticfiles',
     27]
     28}}}
    1029- `python manage.py runserver` works, I can access the admin site and see the changes in templates
    1130- 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`.
     31{{{#!python
     32# myproject/admin.py
     33from django.contrib import admin
     34
     35class MyAdminSite(admin.AdminSite):
     36    index_title = 'Test'
     37
     38
     39# myproject/apps.py
     40from django.contrib.admin.apps import AdminConfig
     41
     42class MyAdminConfig(AdminConfig):
     43    default_site = 'myproject.admin.MyAdminSite'
     44
     45
     46# myproject/settings.py
     47INSTALLED_APPS = [
     48    'myproject',
     49
     50    'myproject.apps.MyAdminConfig',  # 'django.contrib.admin',
     51    'django.contrib.auth',
     52    'django.contrib.contenttypes',
     53    'django.contrib.sessions',
     54    'django.contrib.messages',
     55    'django.contrib.staticfiles',
     56]
     57}}}
    1258- `python manage.py runserver`
    1359
    14 **Actual result**
     60Actual result:
    1561`RuntimeError: 'myproject.apps' declares more than one default AppConfig: 'AdminConfig', 'MyAdminConfig'.``
    1662
    17 **This a regression**
     63Is this a regression?
    1864- `pip install Django==3.1.8`
    1965- `python manage.py runserver`
    2066- All ok, both templates and AdminConfig working at the same time
    2167
    22 **Workaroud**
     68Workaroud
    2369- use `app_admin.py` instead of `apps.py` when following the documentation to override the default admin site
    24 - adjust settings.py: `'myproject.apps_admin.MyAdminConfig'` in INSTALLED_APPS
    25 - works with Django 3.2
     70{{{#!python
     71# myproject/apps_admin.py
     72from django.contrib.admin.apps import AdminConfig
     73
     74class MyAdminConfig(AdminConfig):
     75    default_site = 'myproject.admin.MyAdminSite'
     76}}}
     77- adjust settings.py: `'myproject.apps_admin.MyAdminConfig' in INSTALLED_APPS
     78{{{#!python
     79INSTALLED_APPS = [
     80    'myproject',
     81
     82    'myproject.apps_admin.MyAdminConfig',  # 'django.contrib.admin',
     83    'django.contrib.auth',
     84    'django.contrib.contenttypes',
     85    'django.contrib.sessions',
     86    'django.contrib.messages',
     87    'django.contrib.staticfiles',
     88]
     89}}}
     90- works with Django 3.2!
Back to Top