Changing the AdminSite instance (and class) to use BEFORE the autodiscover runs

116 views
Skip to first unread message

Luis Masuelli

unread,
Jan 22, 2016, 5:38:23 PM1/22/16
to Django users
I would like to create a custom AdminSite instance, using a custom subclass of my own, and provide some project-level urls for the AdminSite. However, I would like to still benefit from the autodiscover feature.

Is there a way I can instance a custom AdminSite subclass, and have that instance reachable as `admin.site` import path? This means, the following line:

from django.contrib.admin import site

returning my custom AdminSite instance, if I want to override it. In this way, installed apps register their model admins against my custom AdminSite instance. For this to work, I want this to be executed before the autodiscover process is run in the app registry.

How can I do it?

Raffaele Salmaso

unread,
Jan 22, 2016, 6:16:06 PM1/22/16
to django...@googlegroups.com
You can use django.contrib.admin.apps.SimpleAdminConfig and call autodiscover yourself.

In your settings
INSTALLED_APPS = [
    ...
    "django.contrib.admin.apps.SimpleAdminConfig",
    ...
    "myproject.apps.Config",
    ...
]

and in myproject/apps.py

from django.apps import AppConfig
from django.utils.translation import ugettext_lazy as _


class Config(AppConfig):
    name = "myproject"
    verbose_name = _("My Project")

    def ready(self):
        super(Config, self).ready()

        from django.contrib import admin
        from myproject.admin.sites import site
        admin.site = site
        admin.autodiscover()



--
Reply all
Reply to author
Forward
0 new messages