In this post, you'll learn how to integrate multiple databases with the Django framework and navigate incoming data using a DB router that automatically writes them to the required database.
Real-world example scenario
Usually, the majority of projects are using relational databases such as Postgres or MySQL but sometimes we also need NoSQL databases to hold extra heavy data which decrease the overload of relational databases.
Assume that your project generates tons of logs while processing some heavy tasks in a queue. These log objects must be stored in non-relational databases instead of hitting relational databases each time and extremely overloading them with huge messy log objects. I guess you spot the problem here, so let's take a look at what can we do about it...
Setting up environment
Create an empty directory named app then let's start by creating a Dockerfile that will copy our working directory and also install required dependencies for Python and Postgres.
These wheel packages will be used while installing and setting up Postgres and other services in the system. Now, we need to add requirements.txt to install the required packages for our project.
We are going to use djongo which will help to convert SQL to MongoDB query. By using djongo we can use MongoDB as a backend database for our Django project. At the time of writing this post, djongo has issues supporting Django versions above v.3+, but it can be resolved easily by just by changing the version from the package itself by forking it to your repository. There is a file named setup.py that holds configuration settings and you'll see the block named install_requires where the supported version is mentioned (Don't forget to fork it first).
We just need to refactor it to fit with our current version of Django. I am using 3.1.3 so I will replace the 3.0.5 to 3.1.3 and it will look like below:
Once you finished, search for requirements.txt and change the Django version there as well:
requirements.txt (in djongo)
...
django>=2.0,<=3.1.3
...
That's it! Commit your changes to the forked repository and then you need to include it to your requirements.txt file but this time it will get from your forked repository like below:
You don't have to go through all these processes because I already included it as you can see from above (requirements.txt) so feel free to use mine if your Django version is 3.1.3 as well.
There are some other dependencies like celery where we'll use it as the queue to pass time-consuming tasks to run in the background and redis is just a message broker for celery. This topic is out of scope for this post but you can visit Dockerizing Django with Postgres, Redis and Celery to understand it more.
Now it's time to set up our services by configuring compose file. Create this file in a root level of your current directory which means it will be outside of app directory There are going to be 5 services in total:
I will not go through this configuration in detail by assuming you already have knowledge about docker and compose files. Simply, we are pulling required images for the services and setting up main environment variables and ports to complete the configuration.
Now we also need to add .env file to fetch values of environment variables while building services:
If you see dbsqlite in project files then you should delete it since we'll use postgres it as a relational database. You also will notice _data directory which represents the volume of MongoDB and Postgres.
Integration with PostgreSQL
We are ready to add our primary relational database which is going to be postgres. Navigate to settings.py and update DATABASES configuration like below:
The default database set to postgres and environment variables will be fetched from .env file. Sometimes, postgres is having connection issues caused by racing issues between Django and postgres. To prevent such situations we'll implement a custom command and add it to commands block in compose file. In this way, Django will wait postgres before launch.
The recommended path of holding commands is /core/management/commands/ from the official documentation of Django. So let's create an app named core then create a management/commands directory inside it.
docker-compose run sh -c "django-admin startapp core"
Then add following command to hold Django until postgres is available:
/core/management/commands/wait_for_db.py
importtimefromdjango.dbimportconnectionsfromdjango.db.utilsimportOperationalErrorfromdjango.core.managementimportBaseCommandclassCommand(BaseCommand):"""Django command to pause execution until db is available"""defhandle(self,*args,**options):self.stdout.write('Waiting for database...')db_conn=Nonewhilenotdb_conn:try:db_conn=connections['default']exceptOperationalError:self.stdout.write('Database unavailable, waititng 1 second...')time.sleep(1)self.stdout.write(self.style.SUCCESS('Database available!'))
Make sure you included __init__.py into sub-directories you created. Now update app service in compose file by adding this command:
Consider the command block only and you'll see we now have two more commands there.
Integration with MongoDB
Actually, the integration of MongoDB is so simple thanks to djongo which handles everything behind the scenes. Switch to settings.py again and we'll add our second database as nonrel which stands for the non-relational database.
The same logic applies here as we did for default database which is postgres.
Setting up DB router
DB router which will automatically write objects to a proper database such as whenever the log object created it should navigate to mongodb instead of postgres. Setting up a DB router is simple, we just need to use router methods that Django provides and define our non-rel models to return the proper database.
Create a new directory named utils inside the core app and also add __init__.py to mark it as a python package. Then add the new file which is DB router below:
/core/utils/db_routers.py
classNonRelRouter:"""
A router to control if database should use
primary database or non-relational one.
"""nonrel_models={'log'}defdb_for_read(self,model,**_hints):ifmodel._meta.model_nameinself.nonrel_models:return'nonrel'return'default'defdb_for_write(self,model,**_hints):ifmodel._meta.model_nameinself.nonrel_models:return'nonrel'return'default'defallow_migrate(self,_db,_app_label,model_name=None,**_hints):if_db=='nonrel'ormodel_nameinself.nonrel_models:returnFalsereturnTrue
nonrel_models - We are defining the name of our models in lowercase which belongs to a non-rel database or mongodb.
db_for_read - the function name is self-explanatory so basically, it is used for reading operations which means each time we try to get records from the database it will check where it belongs and return the proper database.
db_for_write - the same logic applies here. It's used to pick a proper database for writing objects.
allow_migrate - Decided if the model needs migration. In mongodb there is no need to run migrations since it's a non-rel database.
Next, we should add extra configuration in settings.py to activate our custom router:
Great! Our database configurations are finished and now it's time to make a few changes in Django as well before launching everything.
Setting up Celery and Redis
This part is a bit out of scope but since I want to illustrate some real-world app then those tools are always present in projects to handle heavy and time-consuming tasks. Let's add celery to our project, but it should place in our project folder alongside with settings file:
Basically, it will discover all tasks alongside the project and will pass them to the queue. Next, we also need to update __init__.py file inside the current directory, which is our Django project:
Celery requires a broker URL for tasks so in this case, we will use Redis as a message broker. Open your settings file and add the following configurations:
Now try to run docker-compose up -d and all services should start successfully.
Adding models and celery tasks
In this section, we'll pass a new task to the celery queue and test if our DB router works properly and writes objects to the required database. Create a new directory inside the core app named models and add the following file inside it to hold MongoDB models:
As you see it's a very simple Log model where it will let us know what's going on behind the scenes of internal operations of our application. Then add a model for Postgres as well:
Now we need to create a celery task that will generate tons of posts with random values by using Faker generators:
core/tasks.py
importloggingimportrandomfromfakerimportFakerfrom.modelsimportPostfromceleryimportshared_taskfromcore.utils.log_handlersimportLoggingHandlerlogger=logging.getLogger(__name__)logger.addHandler(LoggingHandler())@shared_taskdefcreate_random_posts():fake=Faker()number_of_posts=random.randint(5,100)foriinrange(number_of_posts):try:ifi%5==0:title=Noneelse:title=fake.sentence()description=fake.text()Post.objects.create(title=title,description=description,)exceptExceptionasexc:logger.error("The post number %s failed due to the %s",i,exc)
By adding if statement there we are forcing some of the posts to fail in order to catch exceptions and write them to mongodb. As you noticed, we are using a custom log handler that will write the log data right away after its produced. So, create a new file named log_handlers.py inside utils folder:
core/utils/log_handlers.py
importloggingfromcore.modelsimportLogclassLoggingHandler(logging.Handler):"""Save log messages to MongoDB
"""defemit(self,record):Log.objects.create(message=self.format(record),)
Great! We are almost ready to launch.
Lastly, let's finish creating a very simple view and URL path to trigger the task from the browser and return JSON a response if the operation succeeded.
Include the core app inside INSTALLED_APPS configuration in settings.py then run the migrations and we're done!
docker-compose run app sh -c"python manage.py makemigrations core"
Now, if you navigate to 127.0.0.1:8000 the task will be passed to the queue and post objects will start to generate.
Try to visit admin and you'll see log objects created successfully and by this way we're avoiding overload postgres while it handles only relational data.
Integration of multiple databases with Django framework and navigate incoming data using DB router which automatically writes them to required database.
MongoDB and Postgres integration with Django
This project includes multiple database configurations to launch Django project with Postgres and MongoDB.
Getting Started
This project works on Python 3+ and Django 3.1.3. If you want to change the Django version please follow the link below to get information about it.
If you feel like you unlocked new skills, please share them with your friends and subscribe to the youtube channel to not miss any valuable information.