Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

exposing multiple databases in django admin

My use case requires me to expose multiple databases in the admin site of my django project. Did that following this link: https://docs.djangoproject.com/en/dev/topics/db/multi-db/#exposing-multiple-databases-in-django-s-admin-interface

Here's the code used:

class MultiDBModelAdmin(admin.ModelAdmin):
# A handy constant for the name of the alternate database.
using = 'other'

def save_model(self, request, obj, form, change):
    # Tell Django to save objects to the 'other' database.
    obj.save(using=self.using)

def delete_model(self, request, obj):
    # Tell Django to delete objects from the 'other' database
    obj.delete(using=self.using)

def get_queryset(self, request):
    # Tell Django to look for objects on the 'other' database.
    return super(MultiDBModelAdmin, self).get_queryset(request).using(self.using)

def formfield_for_foreignkey(self, db_field, request, **kwargs):
    # Tell Django to populate ForeignKey widgets using a query
    # on the 'other' database.
    return super(MultiDBModelAdmin, self).formfield_for_foreignkey(db_field, request, using=self.using, **kwargs)

def formfield_for_manytomany(self, db_field, request, **kwargs):
    # Tell Django to populate ManyToMany widgets using a query
    # on the 'other' database.
    return super(MultiDBModelAdmin, self).formfield_for_manytomany(db_field, request, using=self.using, **kwargs)

And then:

admin.site.register(Author, MultiDBModelAdmin)
admin.site.register(Publisher, PublisherAdmin)

othersite = admin.AdminSite('othersite')
othersite.register(Publisher, MultiDBModelAdmin)

The example's documentation states: This example sets up two admin sites. On the first site, the Author and Publisher objects are exposed; Publisher objects have an tabular inline showing books published by that publisher. The second site exposes just publishers, without the inlines.

What I don't seem to find out anywhere is: how do I access the other 'site'? What URL has to be used to view the tables exposed in the other 'site'? Should be something straightforward, but I cannot seem to find it anywhere.

like image 221
Chetan Avatar asked Sep 19 '16 08:09

Chetan


1 Answers

You need to add a url pattern for your admin site, similar to how you enable the regular site:

# urls.py
from django.conf.urls import url
from django.contrib import admin
from myapp.admin import othersite

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^otheradmin/', othersite.urls),
]

You then access the other admin at whatever url you used. In this case, /otheradmin/.

This syntax is for Django 1.10+. On earlier versions of Django, you use include(othersite.urls) instead of othersite.urls.

like image 182
Alasdair Avatar answered Nov 08 '22 06:11

Alasdair