So, this week I just came across an issue related to how to setup multiple databases on Django. Reading the documentation I found it quite trivial to setup. After having the databases configured, you can use a Database Router, if you want to automatically route your app models, or use .using(‘db1′)manually on your queries, specifying the wanted database.

As I didn’t want to specify the databases on the queries in my code, so I decided to use the Database Router, which is, by the way, a very nice solution for such a problem. Looking at the example on the Django docs, I realized that the example was only covering the routing of an unique app. Looking further, I also noticed that it was a bit tied with placeholders related to the app and the database names inside the class. Then I asked myself:

Couldn’t it be more generic and reusable for more than one app?

Searching a bit on the Web I couldn’t find anything. Having that in mind, I came with the following code that can be used to specify several apps using different databases. Everything just using a single settings variable and in a very simple way: