1
|
Multitenant
|
2
|
-----------
|
3
|
|
4
|
An application for making a Django application multitenant for Entr'ouvert
|
5
|
customers.
|
6
|
|
7
|
Based on https://django-tenant-schemas.readthedocs.org/
|
8
|
|
9
|
It is developed, tested and supported on Django 1.7, but it should work with
|
10
|
Django 1.6 + south.
|
11
|
|
12
|
|
13
|
Install
|
14
|
-------
|
15
|
|
16
|
See also : https://django-tenant-schemas.readthedocs.org/
|
17
|
|
18
|
Set the tenant model:
|
19
|
|
20
|
TENANT_MODEL = 'multitenant.Tenant'
|
21
|
|
22
|
Where are tenants:
|
23
|
|
24
|
TENANT_BASE = '/var/lib/<project>/tenants'
|
25
|
|
26
|
Add the middlewares for multitenant, they must be first:
|
27
|
|
28
|
MIDDLEWARE_CLASSES = (
|
29
|
'entrouvert.djommon.multitenant.middleware.TenantMiddleware',
|
30
|
'entrouvert.djommon.multitenant.middleware.JSONSettingsMiddleware',
|
31
|
'entrouvert.djommon.multitenant.middleware.PythonSettingsMiddleware',
|
32
|
) + MIDDLEWARE_CLASSES
|
33
|
|
34
|
Define the shared applications:
|
35
|
|
36
|
SHARED_APPS = (
|
37
|
'tenant_schemas',
|
38
|
'entrouvert.djommon.multitenant',
|
39
|
# those are needed for the public apps to work
|
40
|
# add also any application needed by the public app
|
41
|
'django.contrib.auth',
|
42
|
'django.contrib.contenttypes',
|
43
|
'django.contrib.sessions',
|
44
|
'django.contrib.messages',
|
45
|
'django.contrib.staticfiles',
|
46
|
)
|
47
|
|
48
|
TENANT_APPS = INSTALLED_APPS
|
49
|
|
50
|
INSTALLED_APPS = ('entrouvert.djommon.multitenant',
|
51
|
'tenant_schemas') + INSTALLED_APPS
|
52
|
|
53
|
# or, with Django 1.6 or older:
|
54
|
# INSTALLED_APPS += ('tenant_schemas', 'entrouvert.djommon.multitenant')
|
55
|
|
56
|
Use multitenant database engine:
|
57
|
|
58
|
DATABASES = {
|
59
|
'default': {
|
60
|
'ENGINE': 'tenant_schemas.postgresql_backend',
|
61
|
'NAME': '<db_name>',
|
62
|
},
|
63
|
}
|
64
|
DATABASE_ROUTERS = (
|
65
|
'tenant_schemas.routers.TenantSyncRouter',
|
66
|
)
|
67
|
|
68
|
# With Django 1.6 or older, use multitenant south adapter:
|
69
|
# SOUTH_DATABASE_ADAPTERS = {'default': 'south.db.postgresql_psycopg2'}
|
70
|
|
71
|
Add the multitenant filesystem template loader and configure where the
|
72
|
multitenant templates are located:
|
73
|
|
74
|
TEMPLATE_LOADERS = (
|
75
|
'entrouvert.djommon.multitenant.template_loader.FilesystemLoader',
|
76
|
) + TEMPLATE_LOADERS
|
77
|
TENANT_TEMPLATE_DIRS = (TENANT_BASE,)
|
78
|
|
79
|
TEMPLATE_CONTEXT_PROCESSORS = (
|
80
|
'django.core.context_processors.request',
|
81
|
) + TEMPLATE_CONTEXT_PROCESSORS
|
82
|
|
83
|
|
84
|
Usage
|
85
|
-----
|
86
|
|
87
|
Create a tenant:
|
88
|
manage.py create_tenant www.example.net
|
89
|
Migration of all tenants:
|
90
|
manage.py migrate_schemas
|
91
|
Add a super user to tenant:
|
92
|
manage.py tenant_command createsuperuser --domain www.example.net
|
93
|
|
94
|
Tenants are created in TENANT_BASE directory, for example :
|
95
|
/var/lib/project/tenants/www.example.net/
|
96
|
templates/ <-- override project templates
|
97
|
static/ <-- to be handled by HTTP server
|
98
|
media/
|
99
|
Each tenant is a PostgreSQL schema, named www_example_net
|