Skip to main content

Handle multi-tenancy in Django with no additional global state using schemas.

https://travis-ci.org/charettes/django-tenancy.png?branch=master https://coveralls.io/repos/charettes/django-tenancy/badge.png?branch=master

Installation

Assuming you have django installed, the first step is to install django-tenancy:

pip install django-tenancy

Now you can import the tenancy module in your Django project.

Using django-tenancy

Define a Tenant Model

The tenant model must be a subclass of tenancy.models.AbstractTenant.

For instance, your myapp/models.py might look like:

from tenancy.models import AbstractTenant

class MyTenantModel(AbstractTenant):
   name = models.CharField(max_length=50)
   # other fields
   def natural_key(self):
      return (self.name, )

Important note: the natural_key method must return a tuple that will be used to prefix the model and its database table. This prefix must be unique to the tenant.

Declare the Tenant Model

Now that you have your tenant model, let’s declare in your project in settings.py:

TENANCY_TENANT_MODEL = 'myapp.MyTenantModel'

Run a database synchronization to create the corresponding table:

python manage.py syncdb

Define the tenant-specific models

The tenant-specific models must subclass tenancy.models.TenantModel.

For instance, each tenant will have projects and reports. Here is how myapp/models.py might look like:

from tenancy.models import AbstractTenant, TenantModel

class MyTenantModel(AbstractTenant):
   name = models.CharField(max_length=50)
   # other fields
   def natural_key(self):
      return (self.name, )

class Project(TenantModel):
   name = models.CharField(max_length=50)
   description = models.CharField(max_length=300, blank=True, null=True)

class Report(TenantModel):
   name = models.CharField(max_length=50)
   content = models.CharField(max_length=300, blank=True, null=True)

Playing with the defined models

You can manipulate the tenant and tenant-specific models as any other Django models.

Create a tenant instance

tenant = MyTenantModel.objects.create("myfirsttenant")

Get a tenant-specific model: for_tenant()

<TenantModel>.for_tenant(<AbtractTenantConcreteSubclass instance>)

TenantModel comes with a method that allows you to get the specific AbstractTenantModel for a given Tenant instance. For instance:

tenant_project = Project.for_tenant(tenant)

Create a tenant-specific model instance

tenant_project.objects.create("myfirsttenant_project")

Python 3.5

An issue with circular references between Model objects prevent garbage collection of tenant specific models on tenant deletion.

Metadata

Release files for django-tenancy 0.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-tenancy 0.3
File Size Uploaded
django-tenancy-0.3.tar.gz 21.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-tenancy 0.3
File Interpreter ABI Platform
django_tenancy-0.3-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 49.4 kB

Release files / django-tenancy-0.3.tar.gz

Download URL django-tenancy-0.3.tar.gz
Size 21.4 kB
Tags Source
SHA-256 checksum
How to use checksums
b15cae0e753613f83dca78f772d6c8fd6aa2fe68643557fc32dee4d2f75fea97
BLAKE2b-256 checksum
How to use checksums
4f97517ca14e3650865be24501494d4065adcc88ed330e08bf27c70d64a3b689
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / django_tenancy-0.3-py2.py3-none-any.whl

Download URL django_tenancy-0.3-py2.py3-none-any.whl
Size 27.9 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
d795848604e7c0012c443d0076217e4a550c74f80bb959335365b51e348bcfb4
BLAKE2b-256 checksum
How to use checksums
0920f9f2c1962ff479a588c5f8b68bde41c2cc938ca37326a8915018d07578ae
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page