Building a multi-tenant web application with Django 1.9 framework(Part1)
In this tutorial we are going to build a multi tenant web application with Django 1.9 framework.Multi tenancy is used mainly for SAAS(Software As a Service) products.SAAS is a software delivery model used in the cloud where each customer gets only the service offered by a specific product not the product itself ,the customer gets a relatively isolated environment for its data which other customers don't have access to.
Multitenancy can be achieved using 3 different techniques:
Single web application instance and multiple database
With this multitenancy technique, each customer gets his own database which offers the most secure environment for clients but also it is the most costly technique in term of servers resources.
Single web application instance and multiple schemas
With this multitenancy technique each client or tenant gets his own schema.Schemas are only available with Postgres database system.
Single web application instance,one database and one schema
With multitenancy technique tenants share the same database and the same schema ,the security and separation is handled only by application code.There is no real separation in client's data.This method is less secure but it takes less server resources.
Multitenancy with Django and Postgres
Each technique has its pros and cons.In this tutorial we are going to achieve multitenancy using the second approach (multiple schemas) thus we need to use the Postgres database system ,the framework we are going to use is Django ,the latest version when writing this tutorial is 1.9.
We are not going to reinvent the wheel ,Django has packages, provided by the great community ,available for nearly any functionality you need to implement.The apps we are going to use in this tutorial are:
Django multitenant schema :
This one is a great Django app for implementing the second multitenacy technique ,it uses schemas feature available in Postgres database system.
Django allauth :
For handling user login and registration.It has many features such as email verification,password reset and social login etc.
Now lets start developing our starter project which supports multitenancy,user login and registration.
Installing Django and related packages
First of all we need to setup our working environement by simply creating a new virtual environment and installing Django and necessary packages using PIP from your terminal.
Please note that i'm assuming you are using Ubuntu,which's what i'm using,But don't worry if you are using another operating system it should be nearly the same steps since Python is a portable environment that works across major operating systems ,the major difference you are going to find is when installing Python and its dependencies which is not covered in this tutorial anyway.
So open your terminal and start typing the magic words:
cd into_your_own_working_directory mkdir django_multitenancy_starter_project cd django_multitenancy_starter_project
Now create a virtual env for this project:
Activate this env with:
Now install django with allauth and django-multitenant-schemas
pip install django pip install django-allauth pip install django-multitenant-schemas
Next start your own Django project with:
django-admin startproject .
In this part we have seen some important concepts about multitenancy and how to achieve it.We have also installed our working environment.In the next part of this tutorial we are going to continue developing our started Django 1.9 project to add multitenancy using django-multitenant-schemas and then add user authentication using django-allauth.
RELATED TUTORIALSDjango 2 Ajax CRUD with Python 3.7 and jQuery
Django 2 CRUD Tutorial: Generic Class-Based Views
Angular 6|7 Tutorial — CRUD & Python REST API
Django Authentication — Login, Logout and Password Change/Reset
Python Django JWT — djangorestframework-jwt Example
Django Form Example—Bootstrap 4 UI via django-crispy-forms
Django TemplateView GET|POST Example — URLs, as_view and get_context_data