Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 444
kender Avatar asked Jul 08 '10 18:07

kender


People also ask

Is Django admin good for production?

Django's Admin is amazing. A built-in and fully functional interface that quickly gets in and allows data entry is priceless. Developers can focus on building additional functionality instead of creating dummy interfaces to interact with the database.


2 Answers

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

like image 110
3 revs, 3 users 93% Avatar answered Oct 05 '22 08:10

3 revs, 3 users 93%


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)

like image 28
Claude Vedovini Avatar answered Oct 05 '22 09:10

Claude Vedovini