Skip to main content

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

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-tenancy-0.3a1.tar.gz (20.0 kB view details)

Uploaded Source

Built Distribution

django_tenancy-0.3a1-py2.py3-none-any.whl (27.0 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file django-tenancy-0.3a1.tar.gz.

File metadata

File hashes

Hashes for django-tenancy-0.3a1.tar.gz
Algorithm Hash digest
SHA256 6aa6590d57bff5569904e8e8af98ab7f9164d6165d13a41031bcf18d4720e9f2
MD5 45a1749209ccf96f2e3c53107d2b2ed8
BLAKE2b-256 51eec33c2c538a8f23c68ccf8a34ba5c526fa77598070e117affff5a31b8a7c8

See more details on using hashes here.

File details

Details for the file django_tenancy-0.3a1-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for django_tenancy-0.3a1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 16ebacc55e3d84269747aa9f7a855e5321d2657f68b3f7c8629da41de9f8b69b
MD5 9b1b8139cd819951d5b1307086f41141
BLAKE2b-256 22ef7f92c7e612cc7e4012f7de29760357b78af96be6f1c17bb141acdc9d7c40

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page