Multi Tenant Application using Django – Connect and manage multiple databases to the single application

Share On Facebook
Share On Google Plus
Share On Linkedin
Contact us

     Today I am going to talk about multi-tenancy. What is multi-tenancy and how to make a multi-tenant architecture based application. Multi-tenancy refers to single instance of the application which server multiple tenants. Tenants refer to the group of users who have common access with specific privileges to the software resource. Suppose that there is a simple application connected to the multiple databases, and each database belongs to the specific users or tenant.

Today, I am going to show you how to make a Django Applicaion connect to the multiple database, switching the databases at run time, get data from the multiple databases and also managing the django admin of the each database site. By the database site we refer to the URL where particular database can be managed. Each database has its own corresponding URL or site, eg: testdb1 has admin uri say – http://localhost:8000/testdb1_admin/ or testdb2 has admin uri say – http://localhost:8000/testdb2_admin/.

If you are only interested in the source code, it is hosted in my github account here – https://github.com/tusharcoder/multiDbApp. To set up the project:-

set up django project
set up django project
set up django project

     In the above images you can see how I did the set up of the project and in the following images, see the django admin accessed in the browser

testdb1 django admin

Above Django Admin of testdb1

testdb2 django admin

Above Django Admin of testdb2

Step By Step Development of the project

      Let’s make this application step by step.

  • Make a folder MultiDbApp

  • Make a virtual environment

  • Activate the environment

  • Install django

  • Install mysql client

  • Install django extensions

  • create project by django-admin command

  • 8. create app core

  • register the core app with main app i.e. multiDbApp

    edit in the multiDbApp/settings.py file, add ‘core.apps.CoreConfig’ to the installed apps and also register django_extensions in the INSTALLED_APPS as:-

  • create a models in app core as edit the core/models.py file and update it as:-

  • log in to the mysql server and create databases as follows

  • Add the database settings in the settings.py file in DATABASES setting as:-

  • make migrations of core app by following command:-

  • Run migrations on databases testdb1, testdb2……testdb5. as

  • Run all migrations in default database and createsuperuser as:-

    Above command create the superuser, in my case

  • create a sample command by:-

    and update the code of file core/management/commands/sample.py file as:

    Now rename the sample.py file to populate_db.py

  • type in the commands and populate the databases with the dummy data

    This populates the db with the dummy data

  • Now write view for rendering the data in the web browser, go to file core/views.py file and update it as :-

    In the above view function i.e. getTestData will query the TestData model from all the databases by using using keyword as you see in the for loop that iterates over the tuple containing the database sites name as we updated earlier in the settings.py file in DATABASES setting .

  • Make a file urls.py in the core folder if not already and update it(i.e. file core/urls.py) as:-

  • Now include the core.urls.py file urls in the main app urls, i.e. in multiDBApp/urls.py file update the code as:-

    In the above file we not only include the core/urls.py file but also register the TestData model dynamically in corresponding database admin site backend. But to make it functional we have to make a middleware.py file in core folder and update it(i.e. file core/middleware.py)code as:-

    And register the middleware in the MIDDLEWARE setting as:-

    so final updated code in the multiDbApp/settings.py file is as:-

    As you see in the middleware.py file what WhichDatabaseIsTOUseMIddleware do. When we request the URI of this project it check for the URL if it contains ’_admin’ string. if it has then it get the string just before this ‘_admin’ and set the session with the current requested database site(eg : testdb1 in case of testdb1_admin) and we will then easily access its admin. We register the TestData Model with respective admin sites in the main urls.py file i.e. multiDbApp/urls.py file. Here custom MultiDBModelAdminCommon model Admin class plays a main role. See in the code and you will get it.

  • run in the terminal command

    go to http://127.0.0.1:8000/test/ or http://localhost:8000/test/ and you will see the test data in the browser.
    To access the admin of different database sites as of testdb1, testdb2, testdb3, testdb4, testdb5….
    type in the browser http://127.0.0.1:8000/testdb1_admin/ for testdb1 admin, similarly http://127.0.0.1:8000/testdb2_admin/ for testdb2 admin and so on and type in the username and password, and access the admin, in my case username: admin, password: qazplmq1w2e3r4 .

  •      So, As we see it is very easy to use multiple databases with a single django application and make a multi tenant architecture and many more. To get the full code of this post go to https://github.com/tusharcoder/multiDbApp and download it or clone it. For more, keep visiting this site and also check my git repositories:- https://github.com/tusharcoder?tab=repositories.
    Thanks for reading this tutorial.

Leave a Reply

Your email address will not be published. Required fields are marked *