How to expose your multiple databases for admin site in Django

Syed Jameer
2 min readJul 18, 2020
Photo by Lucas van Oort on Unsplash

Assuming you have multiple databases in your Django project for different apps. Now you realize that when you save or do other operations in the admin site for your app model, you realize that values are save to the default database from the project.

Below we will see simple steps to make admin site of Django to use your other database configured for the app instead of the default.

Now your multiple databases in settings.py may look something like this

Setting.py

So in order to inform the Django admin site use your intended database for your app.

Navigate to your app in the Django project and open your admin.py. The below example shows the model names ProfileSection which when the user saves the information in the admin site uses the default database.

Admin.py

If you want to provide an admin interface for a model on a database other than that specified by your router chain, you’ll need to write custom ModelAdmin classes that will direct the admin to use a specific database for content.

https://docs.djangoproject.com/en/dev/topics/db/multi-db/#exposing-multiple-databases-in-django-s-admin-interface

You can now create a ModelAdmin object in your admin.py like below. Also, mention which database to be used in the “using” keyword

ModelAdmin object in admin.py

Once you have created your custom ModelAdmin setup. You can now proceed to register your model in the admin site to use your other database. In my case “resumeapp” will be used.

Registering model in admin .py

You are all set. Now when you save or do other operations in admin site on your model in the app, Django will use the database mentioned in “using” keyword.

Hope this was helpful.!!

--

--