Serge Mosin
Serge Mosin

Reputation: 396

Django custom AdminSite and ModelAdmin

I have overridden standard AdminSite with my own one:

project/admin/base.py:

from django.contrib import admin    

class MyAdmin(admin.AdminSite):
    ...    

my_admin = MyAdmin()

and registered some application's model with it: fact/admin.py:

from django.contrib import admin
from project.admin.base import my_admin
from models import Fact    

class FactAdmin(admin.ModelAdmin):
    fields = ('section', 'description')


my_admin.register(Fact, FactAdmin)

How can I get this model displayed on an index page? I can't use autodiscover as stated here: How to use custom AdminSite class? The workaround suggested in that question also doesn't work for me and I'd like to make it it a more clean way.

The documentation only states that

There is really no need to use autodiscover when using your own AdminSite instance since you will likely be importing all the per-app admin.py modules in your myproject.admin module.

I don't import per app modules in project.admin module though. So is there any way to tell custom AdminSite about the registered model and show it on the index?

Edit: I have hooked URLs and I see my_admin, not admin. With standard admin everything works correctly. Here is the code of project/urls.py:

from django.conf.urls import patterns, include, url

from admin.base import my_admin

urlpatterns = patterns('',
    ...    
    url(r'^admin/', include(my_admin.urls, app_name='admin')),
)

Upvotes: 1

Views: 3617

Answers (1)

Rafa He So
Rafa He So

Reputation: 473

I think you need to do:

from django.contrib import admin  
class MyAdmin(admin.AdminSite):
  ...    

admin.site = MyAdmin()

Upvotes: 3

Related Questions