Easy to create applications that use tenants in django
Project description
easy-tenants
This is a Django app for managing multiple tenants on the same project instance using a shared approach.
Background
There are typically three solutions for solving the multitenancy problem:
- Isolated Approach: Separate Databases. Each tenant has it’s own database.
- Semi Isolated Approach: Shared Database, Separate Schemas. One database for all tenants, but one schema per tenant.
- Shared Approach: Shared Database, Shared Schema. All tenants share the same database and schema. There is a main tenant-table, where all other tables have a foreign key pointing to.
This application implements the third approach, which in our opinion, is the best solution for a large amount of tenants.
For more information: Building Multi Tenant Applications with Django
Below is a demonstration of the features in each approach for an application with 5000 tenants.
Approach | Number of DB | Number of Schemas | Django migration time | Public access |
---|---|---|---|---|
Isolated | 5000 | 5000 | slow (1/DB) | No |
Semi Isolated | 1 | 5000 | slow (1/Schema) | Yes |
Shared | 1 | 1 | fast (1) | Yes |
How it works
The following image shows the flow of how this application works.
Instalation
Assuming you have django installed, the first step is to install django-easy-tenants
.
pip install django-easy-tenants
Now you can import the tenancy module in your Django project.
Setup
It is recommended to install this app at the beginning of a project. In an existing project, depending on the structure of the models, the data migration can be hard.
Add easy_tenants
to your INSTALLED_APPS
on settings.py
.
settings.py
INSTALLED_APPS = [
...,
'easy_tenants',
]
Create a model which will be the tenant of the application.
yourapp/models.py
from easy_tenants.models import TenantMixin
class Customer(TenantMixin):
...
Define on your settings.py
which model is your tenant model. Assuming you created Customer
inside an app named yourapp
, your EASY_TENANTS_MODEL should look like this:
settings.py
EASY_TENANTS_MODEL = 'yourapp.Customer'
Your models, that should have data isolated by tenant, need to inherit from TenantAbstract
and the objects need to be replaced by TenantManager()
.
from django.db import models
from easy_tenants.models import TenantAbstract
from easy_tenants.managers import TenantManager
class Product(TenantAbstract):
name = models.CharField(max_length=10)
objects = TenantManager()
To obtain the data for each tenant, it is necessary to define which tenant will be used:
from easy_tenants import set_current_tenant
customer = Customer.objects.first()
set_current_tenant(customer)
Product.objects.all() # filter by customer
or
from easy_tenants import tenant_context
with tenant_context(customer):
Product.objects.all() # filter by customer
To define the tenant to be used, this will depend on the business rule used. Here is an example for creating middleware that defines a tenant:
from django.http import HttpResponse
from easy_tenants import tenant_context
class TenantMiddleware:
def __init__(self, get_response):
self.get_response = get_response
def __call__(self, request):
customer = get_customer_by_request(request)
if not customer:
return HttpResponse("Select tenant")
with tenant_context(customer):
return self.get_response(request)
If you want to separate the upload files by tenant, you need to change the DEFAULT_FILE_STORAGE
configuration (only available for local files).
DEFAULT_FILE_STORAGE = 'easy_tenants.storage.TenantFileSystemStorage'
Running the example project
python manage.py migrate
python manage.py createsuperuser
python manage.py shell # create 2 customers
python manage.py runserver
Access the page /admin/
, create a Customer
and then add a user on the created Customer
.
Motivation
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for django-easy-tenants-0.4.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 32b8cbc8ec916575c44f7c9db9bd54a9780b21615fc8b706aa486c326e5d8979 |
|
MD5 | 970369a3f4034d18b78d4cb232e47628 |
|
BLAKE2b-256 | c18c8b2b984a0ca368e4e04b9c0fb0af5061166759d27ad15eb0a9847d4a1fa4 |
Hashes for django_easy_tenants-0.4.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4257fb30d327ffe114039873e4199f056cbfc259d04a891ec0c18530bd62bb57 |
|
MD5 | 554640f144ca5d362f46c095b2a49096 |
|
BLAKE2b-256 | e69b3f5ba121af19f4bd7c4fae22fa8041da7f1d2d1ee66282a9553bbd1b3ebc |