How to have 2 different admin sites in a Django project?

前端 未结 3 1625
终归单人心
终归单人心 2020-12-02 13:56

I want to have 2 separate admin sites inside a Django project.

By separate I mean - they should have separate users authentication, they should administer different

3条回答
  •  我在风中等你
    2020-12-02 14:18

    You can subclass Django's AdminSite (put it eg. in admin.py in your project root):

    from django.contrib.admin.sites import AdminSite
    
    class MyAdminSite(AdminSite):
        pass
        #or overwrite some methods for different functionality
    
    myadmin = MyAdminSite(name="myadmin")   
    

    At least from 1.9 on you need to add the name parameter to make it work properly. This is used to create the revers urls so the name has to be the one from the urls.py.

    Then you can use it in your app's admin.py the same way as you do with the normal AdminSite instance:

    from myproject.admin import myadmin
    myadmin.register(MyModel_A)
    

    You also need to define some urls for it (in your project's urls.py):

    from myproject.admin import admin, user_site
    from myproject.admin import myadmin
    urlpatterns = patterns('',
        ...
        (r'^admin/', include(admin.site.urls)),
        (r'^myadmin/', include(myadmin.urls)),
    

    Also see this: http://docs.djangoproject.com/en/dev/ref/contrib/admin/#adminsite-objects

提交回复
热议问题