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

14,838

Solution 1

To register models in different AdminSites you just need to create different instances of django.contrib.admin.sites.AdminSite, see this.

You will be good to go with two different admin sites managing different models and having different templates. For authentication and permissions you should be able to use the build-in django.contrib.auth as is with custom permissions (hope someone else will be able to help more here)

Solution 2

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

Share:
14,838

Related videos on Youtube

kender
Author by

kender

Programmer by heart, currently freelance developer on mostly iOS applications and python web projects. Basic field of interest: python coding, obj-c, iOS, java, distributed databases, bussiness usage of python :) Also interested in usability and integration testing.

Updated on November 12, 2020

Comments

  • kender
    kender almost 3 years

    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 models, and have different looks and URLs.

    The reason I want to do it is the customer wants separate section to administer the CMS part of the page, and separate to use as a 'back-office' solution.

    I thought about just making a copy od django.contrib.auth appliaction in my project tree, naming it differently and using separate admin.site.register() calls for both of them. This way I can have other models available in each one of them, diffrent looks, etc. I don't know how to solve the user-authentication problem (I should have different user to be able to log into CMS then into the BackOffice).

    Anyone happened to do this before and could give me some hint? Or what I plan to do is just wrong by design?

  • kender
    kender over 13 years
    when I try to do this, after I log in, I get the "You don't have permission to edit anything." message...
  • Claude Vedovini
    Claude Vedovini over 13 years
    The user you use should have the is_staff and is_superuser fields set to true. Then after to distinguish between different admin users and what they have access to see docs.djangoproject.com/en/1.2/topics/auth/#permissions
  • kender
    kender over 13 years
    Ok, I got this to work. But I can't seem to have a different set of templates for 2 admin sites - they both look up the 'admin/' directory in the templates, even one is created with 'backoffice' argument, which should set its name to 'backoffice'...
  • Robert
    Robert about 5 years
    How to register MyAdminSite in the InstalledApps? Basically, I have Tenant and Shared apps. Created new instance of the admin and need to register only this new instance to the shared apps.