django-adminplus icon indicating copy to clipboard operation
django-adminplus copied to clipboard

Autodiscover from readme doesn't work correctly

Open nitr0man opened this issue 2 years ago • 1 comments

when I tried to do autodiscover like in manual - it doesn't work correctly (a lot of models wasn't discovered).

I've overrided default admin site according to Django manual https://docs.djangoproject.com/en/4.0/ref/contrib/admin/#overriding-default-admin-site and it works OK.

Please update your manual.

nitr0man avatar Jan 05 '24 09:01 nitr0man

For those that find this issue, here is a full example of the solution indicated by @nitr0man above:

The installation instructions for django-adminplus are seemly outdated for Django 3.2+. While it mostly works, it can fail in seemingly random situations due to inconsistencies in the order of application loading in Django. For example, we would occationally review these check errors:

SystemCheckError: System check identified some issues:
ERRORS:
<class 'filer.admin.fileadmin.FileAdmin'>: (admin.E039) An admin for model "User" has to be registered to be referenced by FileAdmin.autocomplete_fields.
<class 'filer.admin.permissionadmin.PermissionAdmin'>: (admin.E039) An admin for model "Group" has to be registered to be referenced by PermissionAdmin.autocomplete_fields.
<class 'filer.admin.permissionadmin.PermissionAdmin'>: (admin.E039) An admin for model "User" has to be registered to be referenced by PermissionAdmin.autocomplete_fields.

AdminPlus overrides the default AdminSite class. Newer Django versions have a revised way to create a custom AdminSite, as documented here: https://docs.djangoproject.com/en/4.2/ref/contrib/admin/#overriding-default-admin-site

To resolve the warnings, you need to create a custom AdminConfig that defines your custom AdminSitePlus:

config/admin.py:

from adminplus.sites import AdminSitePlus
from django.conf import settings
from django.contrib.admin.apps import AdminConfig


class CustomAdminSitePlus(AdminSitePlus):
    site_header = f'{settings.APPLICATION_NAME} - {settings.ENVIRONMENT}'
    site_title = f'{settings.APPLICATION_NAME} - {settings.ENVIRONMENT}'

class CustomAdminConfig(AdminConfig):
    default_site = "config.admin.CustomAdminSitePlus"
    # If you don't have a CustomAdminSitePlus class, you can directly point to AdminPlus:
    # default_site = "adminplus.sites.AdminSitePlus"

Then, in your settings.py, replace "django.contrib.admin" with your custom AdminConfig in INSTALLED_APPS:

INSTALLED_APPS = [
    # Other apps...
    'config.admin.CustomAdminConfig',
    'adminplus',
    # More apps...
]

peterfarrell avatar Apr 24 '25 13:04 peterfarrell