How to Use Multiple Databases in Django Admin Interface

This was a problem I ran into recently, and here’s a simple solution to it. The Django doc’s example handles this problem by making 2 admin sites, but I wanted to handle 2 databases on1 admin dashboard.

In project_name/app_name/admin.py we can register our objects to be on the localhost:8000/admin interface. All we need to change here is add a class MultiDBModdelAdmin from the docs, and also a class that specifies the name of our database we want for the models in this app:

# admin.py of one of your apps
from django.contrib import admin
from .models import Article, Author
class MultiDBModelAdmin(admin.ModelAdmin):
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().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().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().formfield_for_manytomany(db_field, request, using=self.using, **kwargs)
# Replace 'OtherDatabase' and 'otherdatabase' with your database's name
class OtherDatabaseAdmin:
using = 'otherdatabase'

Lastly, in the same admin.py file, we can register our models just as normal. Except we need to add a 2nd parameter that is using our new class OtherDatabaseAdmin .

admin.site.register(Article, OtherDatabaseAdmin)
admin.site.register(Author, OtherDatabaseAdmin)

You can explicitly check specific databases in python manage.py shell by using:

from my_app.models import Articles
Articles.objects.using('otherdatabase').all()

Or you can manually check inside the actual database.

After you have completed the admin.py modifications, you should be able to see, create, edit, and delete the objects to the specified database.

Troubleshooting

  1. If you’re running into issues, check to make sure your database name is correct. It is the key to your database settings in your project_name/project_name/settings.py file:
# project_name/project_name/settings.pyDATABASES = {
"otherdatabase": {
"ENGINE": "django.db.backends.mysql",
"NAME": "blog_db",
"USER": "hotstuff",
"PASSWORD": "reallylongpassword",
"HOST": "",
"PORT": "",
}
}

In the code above, our database name will be otherdatabase , not blog_db .

2. Another common issue is not migrating to the specified database:

python manage.py makemigrations
python manage.py migrate --database=otherdatabase

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store