100 lines
2.8 KiB
Plaintext
100 lines
2.8 KiB
Plaintext
Multitenant
|
|
-----------
|
|
|
|
An application for making a Django application multitenant for Entr'ouvert
|
|
customers.
|
|
|
|
Based on https://django-tenant-schemas.readthedocs.org/
|
|
|
|
It is developed, tested and supported on Django 1.7, but it should work with
|
|
Django 1.6 + south.
|
|
|
|
|
|
Install
|
|
-------
|
|
|
|
See also : https://django-tenant-schemas.readthedocs.org/
|
|
|
|
Set the tenant model:
|
|
|
|
TENANT_MODEL = 'multitenant.Tenant'
|
|
|
|
Where are tenants:
|
|
|
|
TENANT_BASE = '/var/lib/<project>/tenants'
|
|
|
|
Add the middlewares for multitenant, they must be first:
|
|
|
|
MIDDLEWARE_CLASSES = (
|
|
'entrouvert.djommon.multitenant.middleware.TenantMiddleware',
|
|
'entrouvert.djommon.multitenant.middleware.JSONSettingsMiddleware',
|
|
'entrouvert.djommon.multitenant.middleware.PythonSettingsMiddleware',
|
|
) + MIDDLEWARE_CLASSES
|
|
|
|
Define the shared applications:
|
|
|
|
SHARED_APPS = (
|
|
'tenant_schemas',
|
|
'entrouvert.djommon.multitenant',
|
|
# those are needed for the public apps to work
|
|
# add also any application needed by the public app
|
|
'django.contrib.auth',
|
|
'django.contrib.contenttypes',
|
|
'django.contrib.sessions',
|
|
'django.contrib.messages',
|
|
'django.contrib.staticfiles',
|
|
)
|
|
|
|
TENANT_APPS = INSTALLED_APPS
|
|
|
|
INSTALLED_APPS = ('entrouvert.djommon.multitenant',
|
|
'tenant_schemas') + INSTALLED_APPS
|
|
|
|
# or, with Django 1.6 or older:
|
|
# INSTALLED_APPS += ('tenant_schemas', 'entrouvert.djommon.multitenant')
|
|
|
|
Use multitenant database engine:
|
|
|
|
DATABASES = {
|
|
'default': {
|
|
'ENGINE': 'tenant_schemas.postgresql_backend',
|
|
'NAME': '<db_name>',
|
|
},
|
|
}
|
|
DATABASE_ROUTERS = (
|
|
'tenant_schemas.routers.TenantSyncRouter',
|
|
)
|
|
|
|
# With Django 1.6 or older, use multitenant south adapter:
|
|
# SOUTH_DATABASE_ADAPTERS = {'default': 'south.db.postgresql_psycopg2'}
|
|
|
|
Add the multitenant filesystem template loader and configure where the
|
|
multitenant templates are located:
|
|
|
|
TEMPLATE_LOADERS = (
|
|
'entrouvert.djommon.multitenant.template_loader.FilesystemLoader',
|
|
) + TEMPLATE_LOADERS
|
|
TENANT_TEMPLATE_DIRS = (TENANT_BASE,)
|
|
|
|
TEMPLATE_CONTEXT_PROCESSORS = (
|
|
'django.core.context_processors.request',
|
|
) + TEMPLATE_CONTEXT_PROCESSORS
|
|
|
|
|
|
Usage
|
|
-----
|
|
|
|
Create a tenant:
|
|
manage.py create_tenant www.example.net
|
|
Migration of all tenants:
|
|
manage.py migrate_schemas
|
|
Add a super user to tenant:
|
|
manage.py tenant_command createsuperuser --domain www.example.net
|
|
|
|
Tenants are created in TENANT_BASE directory, for example :
|
|
/var/lib/project/tenants/www.example.net/
|
|
templates/ <-- override project templates
|
|
static/ <-- to be handled by HTTP server
|
|
media/
|
|
Each tenant is a PostgreSQL schema, named www_example_net
|