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.
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
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
File details
Details for the file django-tenancy-0.3.1a2.tar.gz
.
File metadata
- Download URL: django-tenancy-0.3.1a2.tar.gz
- Upload date:
- Size: 21.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f87d2923f7bac38b9f00192f4d91acbc018380bf644dff7528eee5a3b1acce12 |
|
MD5 | dfeac258f910b4723a7b8f2a6a024124 |
|
BLAKE2b-256 | ded32eaddc106356b9e06aae68fd3d3ecc2a58699a97c4c58a71b059a0e7e41a |
File details
Details for the file django_tenancy-0.3.1a2-py2.py3-none-any.whl
.
File metadata
- Download URL: django_tenancy-0.3.1a2-py2.py3-none-any.whl
- Upload date:
- Size: 28.5 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 214ebab8b464585a6ccf2a123e773e0af025a371b31fb937a279c35ee1c711f0 |
|
MD5 | 653e64e82db7a3b54c4f57ac0ebda1e2 |
|
BLAKE2b-256 | 0327364d2e3bed095d92794ec24a7c3f16985f43baec2f68b1ec6dd5e0d43f98 |