How to implement Multitenet Applications Using Django

[2/1, 6:56 PM] Etentlabs: Greetings,

[2/1, 6:57 PM] Etentlabs: The program will be starting shortly

[2/1, 7:05 PM] Etentlabs: The outline of our program will be:

[2/1, 7:07 PM] Etentlabs: 1.what is SaaS

[2/1, 7:07 PM] Etentlabs: 2. Important concept in Saas

[2/1, 7:10 PM] Etentlabs: 3. Relationship of multitenancy and the SaaS model

[2/1, 7:13 PM] Etentlabs: 4. Schematic architecture

[2/1, 7:14 PM] Etentlabs: 5. The tenant and the domain model

[2/1, 7:14 PM] Etentlabs: 6. Django and the tenent

[2/1, 7:25 PM] Etentlabs: Software as a service  (SaaS software licensing) is a delivery model in which software is licensed on a subscription basis and is centrally hosted. It is sometimes referred to as "on-demand software"

[2/1, 7:30 PM] Etentlabs: Software as a service (SaaS) will remain the largest market segment of all public cloud offering, which is forecast to grow to $116 billion in 2020 due to the scalability of subscription-based software -- Gartner

[2/1, 7:31 PM] Etentlabs: Growing from $72 billion in 2018

[2/1, 7:32 PM] Etentlabs: So I suggest now is the time to have a slice of the cake πŸ˜‚πŸ˜‚πŸ˜‚

[2/1, 7:37 PM] Etentlabs: Setup cost for SaaS is typically lower than the equivalent enterprise software. 


Building Saas you will  adopt pricing your applications based on some usage parameters, such as the number of users using the application. 


However, because in a SaaS environment customers' data reside with the SaaS developing company, opportunities also exist to charge per transaction, event, or other units of value, such as the number of virtual machines, data storage, and even uptime.

[2/1, 7:39 PM] Etentlabs: Section 2: Important concepts in SaaS

[2/1, 7:41 PM] Etentlabs: 1. User provisioning

[2/1, 7:42 PM] Etentlabs: 2. Multitenent

[2/1, 7:42 PM] Etentlabs: 3. Horizontal Scaling

[2/1, 7:43 PM] Etentlabs: 4. Vertical SaaS and horizontal SaaS

[2/1, 7:45 PM] Etentlabs: 5. Data escrow


[2/1, 7:49 PM] Etentlabs: With this quotes from sales force CEO, I end section 2

[2/1, 7:50 PM] Etentlabs: SECTION 3: Relationship of multitenancy with the SasS model

[2/1, 7:52 PM] Etentlabs: Have you ever imagine the connection of the human mind with the matter around us?  


If you have then you can begin to fathom the link between Multitenancy and the SaaS model

[2/1, 7:54 PM] Etentlabs: Even though both are abstract concepts...


 Their link and their  comprehension has brought about the greatest companies of the last decade,

[2/1, 7:56 PM] Etentlabs: "Software multitenancy" refers to a software architecture  in which a single instance of software runs on a server and serves multiple tenants.


 Systems designed in such manner are often called  shared (in contrast to dedicated or isolated). 


A tenant is a group of users who share a common access with specific privileges to the software instance.

[2/1, 8:01 PM] Etentlabs: For example imagine you build a django application with the capability for signup by a set of users at the imaginary site:

[2/1, 8:02 PM] Etentlabs: Now imagine that that same application exist at with distinct sets of users

[2/1, 8:05 PM] Etentlabs: The magic of SaaS multitent application is that both applications are the same application from the same deployment but they share a different instance of the database hence they are different tenant

[2/1, 8:09 PM] Etentlabs: Section 4: Schematic Architecture

[2/1, 8:14 PM] Etentlabs: What is schema


[2/1, 8:15 PM] Etentlabs: A schema can be seen as a directory in an operating system, each directory (schema) with it’s own set of files (tables and objects). 


This allows the same table name and objects to be used in different schemas without conflict.

[2/1, 8:18 PM] Etentlabs: A database contains one or more named schemas, which in turn contain tables. 


Schemas also contain other kinds of named objects, including data types, functions, and operators. 


The same object name can be used in different schemas without conflict; for example, both schema1 and myschema can contain tables named mytable.


 Unlike databases, schemas are not rigidly separated: a user can access objects in any of the schemas in the database he is connected to, if he has privileges to do so.

[2/1, 8:21 PM] Etentlabs: From the above we can see the schematic architecture of a SaaS app

[2/1, 8:25 PM] Etentlabs: Section 5: The tenant and the domain model

[2/1, 8:27 PM] Etentlabs: Each tenant of the same application should have a domain and domain name associated with it and this should be done automagically by the parent app

[2/1, 8:27 PM] Etentlabs:

[2/1, 8:27 PM] Etentlabs:

[2/1, 8:28 PM] Etentlabs: Till

[2/1, 8:29 PM] Etentlabs: All this stemming from should be automagically deployed by the parent app at


This is the beauty of a true SaaS model

[2/1, 8:32 PM] Etentlabs: some model uses an hash key to have access to the tenant


Exactly the way Google does for it web apps

[2/1, 8:36 PM] Etentlabs: Tenants are identified via their host name (i.e


 This information is stored on a table on the public schema.


 Whenever a request is made, the host name is used to match a tenant in the database. 


If there’s a match, the search path is updated to use this tenant’s schema. So from now on all queries will take place at the tenant’s schema.


 For example, suppose you have a tenant customer at Any request incoming at will automatically use coronavirus’s schema and make the tenant available at the request.


 If no tenant is found, a 404 error is raised. This also means you should have a tenant for your main domain, typically using the public schema.

[2/1, 8:39 PM] Etentlabs: SECTION 6: Django and the tenant

[2/1, 8:40 PM] Etentlabs: this is the juice and the final part of today's session

[2/1, 8:41 PM] Etentlabs: To implement multitenancy with your current application you will have to pip install

[2/1, 8:41 PM] Etentlabs: pip install django-tenants

[2/1, 8:42 PM] Etentlabs: You’ll have to make the following modifications to your file.


 Your DATABASE_ENGINE setting needs to be changed to 


DATABASES = { 'default': { 'ENGINE': 'django_tenants.postgresql_backend', # .. } }

[2/1, 8:43 PM] Etentlabs: Add django_tenants.routers.TenantSyncRouter to your DATABASE_ROUTERS setting, so that the correct apps can be synced, depending on what’s being synced (shared or tenant).


 DATABASE_ROUTERS = ( 'django_tenants.routers.TenantSyncRouter', )

[2/1, 8:44 PM] Etentlabs: Add the middleware django_tenants.middleware.main.TenantMainMiddleware to the top of MIDDLEWARE, so that each request can be set to use the correct schema.


 MIDDLEWARE = ( 'django_tenants.middleware.main.TenantMainMiddleware', (

[2/1, 8:46 PM] Etentlabs: Make sure you have django.template.context_processors.request listed under the context_processors option of TEMPLATES otherwise the tenant will not be available on request.



[2/1, 8:49 PM] Etentlabs: Question session:


Django Replies 0 Views 202 Users 0

Suggested Topics

Django Updated on April 1, 2020, 9:12 p.m. Replies Views 55

Django Updated on April 6, 2020, 12:50 a.m. Replies Views 43

Django Updated on April 7, 2020, 9:54 p.m. Replies Views 74

Django Updated on April 13, 2020, 11:35 a.m. Replies Views 143