Using Django with Multiple Databases

Gajanan Rajput
4 min readMar 2, 2024

Django, a high-level Python web framework, promotes rapid development and clean, pragmatic design. A common scenario for many Django projects, especially as they scale, involves using multiple databases. This could be for separating read and write operations, handling different types of data, or working with legacy systems. Understanding how to configure and use multiple databases in Django is crucial for developers looking to optimize their projects for scalability and performance.

Using multiple databases in a Django project allows you to scale your application, separate concerns (such as separating read and write operations, or keeping different types of data in different databases), and work with legacy systems. Here’s how to approach this task, keeping in mind the steps you outlined: Project, Choose the suitable model and dataset, Fine-tuning, Evaluations, and Deployment.

👉1. Project Setup

First, ensure your Django project is set up correctly. You’ll need Django installed and a project initialized. If you haven’t done this, you can start by creating a new Django project:

django-admin startproject multiple_db_example
cd multiple_db_example

👉2. Configuring Multiple Databases

In your settings.py, configure the DATABASES setting to include multiple databases. For example:

DATABASES = {
'default': {},
'users': {
'NAME': 'local_db',
'ENGINE': 'django.db.backends.postgresql',
'USER': 'your_username',
'PASSWORD': 'your_password',
},
'inventory': {
'NAME': 'production_db',
'ENGINE': 'django.db.backends.mysql',
'USER': 'your_username',
'PASSWORD': 'your_password',
},
}

👉3. Choose the Suitable Model and Dataset

Decide which models should be stored in which database. This could depend on the nature of the data, performance considerations, or legacy system requirements. For example, user-related models might go into the users database, while product inventory models might go into the inventory database.

Create your models in the models.py file of your app. For instance, a User model may look like this:

from django.db import models
class User(models.Model):
name = models.CharField(max_length=100)
email = models.EmailField(

And a Product model:

class Product(models.Model):
name = models.CharField(max_length=100)
description = models.TextField()
price = models.DecimalField(max_digits=10, decimal_places=2)

👉4. Routing Database Calls

You’ll need to create a database router that Django can use to determine which database to use for each model. Here is an example router:

class AuthRouter:
"""
A router to control all database operations on models in the
auth and contenttypes applications.
"""
route_app_labels = {'auth', 'admin', 'contenttypes', 'sessions', 'messages', 'staticfiles'}
def db_for_read(self, model, **hints):
"""
Attempts to read auth and contenttypes models go to auth_db.
"""
if model._meta.app_label in self.route_app_labels:
return 'users'
return 'default'
def db_for_write(self, model, **hints):
"""
Attempts to write auth and contenttypes models go to auth_db.
"""
if model._meta.app_label in self.route_app_labels:
return 'users'
return 'default'
def allow_relation(self, obj1, obj2, **hints):
"""
Allow relations if a model in the auth or contenttypes apps is
involved.
"""
if (
obj1._meta.app_label in self.route_app_labels or
obj2._meta.app_label in self.route_app_labels
):
return True
return None
def allow_migrate(self, db, app_label, model_name=None, **hints):
"""
Make sure the auth and contenttypes apps only appear in the
'users' database.
"""
if app_label in self.route_app_labels:
return db == 'users'
return None

Then, add your router to the DATABASE_ROUTERS setting in settings.py:

DATABASE_ROUTERS = ['path.to.AuthRouter']

👉Fine-Tuning the Database Setup

Optimization comes next. Consider using connection pooling, indexing, and read replicas to enhance performance. Django’s ORM allows for sophisticated querying and optimization techniques, such as using select_related and prefetch_related for efficient data retrieval.

👉Working with Models Across Databases

When saving or retrieving models, you may specify the database:

# Saving a model instance to the 'analytical' database
report = SalesReport(name="Monthly Sales")
report.save(using='analytical')
# Retrieving all instances from the 'transactional' database
orders = Order.objects.using('transactional').all()

👉Fine-tuning

Database Operations

When working with multiple databases, you might need to perform operations such as migrations, which by default, only apply to the default database. To run migrations on a specific database, use:

python manage.py migrate --database=users

Conclusion

Leveraging multiple databases in Django allows for more flexible, efficient handling of diverse data types and can significantly enhance your project’s performance and scalability. By following the steps outlined in this guide, from project setup to deployment, you’ll be well-equipped to take advantage of this powerful feature of Django.

Ready to take your Django projects to the next level? Embrace the power of multiple databases and watch your applications thrive in complexity and efficiency.

This guide aims to demystify the process of integrating multiple databases into a Django project, providing you with a solid foundation to build upon. Whether you’re managing large datasets, requiring different types of databases, or simply aiming for optimal performance, this approach can cater to your needs. Happy coding, and may your projects flourish with the added power of multiple databases!

The article was originally published at mrcoder701 blog

Leave a response to this article by providing your insights, comments, or requests for future articles.

Share the articles with your friends and colleagues on social media.

Let’s Get in Touch! Follow me on 💞:

>GitHub: @gajanan0707

>Linkedin: Gajanan Rajput

>Website: https://mrcoder701.com

--

--