Django multi-tenancy, without any core patches required!
Perfect for SAAS, saving memory, or other insane mad ideas.
pip install -e 'git+https://github.com/radiosilence/django-squatter.git'
Add these to settings:
DATABASE_ROUTERS = (
'squatter.routers.TenancyRouter',
...
)
MIDDLEWARE_CLASSES = (
...
'squatter.middleware.TenancyMiddleware',
)
TEMPLATE_LOADERS = (
'squatter.template.loaders.Loader',
)
INSTALLED_APPS = (
...
'squatter',
...
)
Do a migration, etc.
These are our more recent internal notes and need editing. But they are better than nothing.
Besides the steps for ordinary projects, we need to create multiple databases for the tenants. As there's no way to sync db except for the main tenant, we need to manually copy the database structrue from main tenant to other tenants. In this case: We created mytenant1, mytenant2 etc And we have existing master db, copy the database into the dbs we just created.
Also we need the media dir and filebrowser dir for each tenant: cd /path/to/mytenant/media mkdir mytenant mkdir mytenant/documents
Say we are adding a new tenant 'mytenant'.
-
create a mytenant directory under myproject/templates, and put the tenant specific template here. in this case we have base.html for a extra style file, and home.html for different layout from ldevents
-
put tenant static in myproject/custom_site/static/mytenant. in this case we have css/mytenant.css
-
create media dir create {{ project_dir }}/media/mytenant
-
add site data at /admin/sites/site/add/, and remember the site id. in this case add www.mytenant.com, id is 6,
-
add the tenant and tenant mapping data at /admin/squatter/ tenant.alias is the most important, we need to use it in TENANT_SETTINGS of next step. in this case we use 'mytenant'
-
add settings to settings_tenants.TENANTS_SETTINGS. For example:
'mytenant': { 'SITE_ID': 6, 'PREPEND_WWW': False or True, 'SITE_NAME': 'MyTenant', 'SITE_DOMAIN': 'www.mytenant.com', 'SITE_MANAGERS' : (('Someone', 'someone@example.org'),), 'JOHNNY_MIDDLEWARE_KEY_PREFIX':'jp_mytenant', 'JIMMY_PAGE_CACHE_PREFIX': 'jp_mytenant', 'MEDIA_URL': 'http://www.mytenant.com/media/', 'STATIC_URL': 'http://www.mytenant.com/static/', 'STATIC_ROOT': '/path/to/mysite/static', 'MEDIA_ROOT': '/path/to/mysite/media/mytenant', },
The file is settings_tenants.py.
Simply find the entry in TENANTS_SETTINGS i.e.
'GOOGLE_WEBMASTER_ID' : 'something',
'GOOGLE_ANALYTICS_ID' : 'UA-101234-56',
Different settings for each tenant?
step 6 above, we put them in settings_tenants.py
Current issues/bugs?
Main issue is we can not run cronjobs for the tenants etc.
- Docs
- Schemas option
- Tests
- Some way of doing things like migrate/syncdb from the command line.
- Neelesh Shastry - The guy who inspired the whole thing with that Posterous article: https://github.com/neeleshs
- https://github.com/bruth
- Andy Baker