Python / Django multi-tenancy solution
Asked Answered
M

4

13

I could use some help creating a plan of attack for a project I'm working on.

Imagine that the site is for a group that oversees regional sales offices, distributed around the world. The purpose of this project is to let superusers spin up a new sub-site specific to each and every office, at a fast pace -- sites are added on a frequent basis. The office sub-sites should be wholly contained with "admin" users specific to that sub-site and should be user-friendly CMS. A superuser should be able to step in and manage all of these office sub-sites.

In addition to the self-contained office sub-site instance, there is also a need for each sub-site to manage contacts, leads, etc and store this in one central area for the superusers.

I've done a few sites using Django, but never anything multi-tenant. I'd like suggestions for technologies to use or tutorials/documentation that might be helpful.

Requirements:

  1. Each sub-site uses the same source (templates, JS, available features, etc), but can be modified to reflect custom content within the templates.
  2. Assigned subdomains (with an option of using a fully qualified domain) per sub-site, configured within the project, not in a hardcoded settings file.
  3. Sub-site specific user access controls, in addition to superusers who can access all sub-sites.
  4. The ability to provide an "independent" CMS for each sub-site. i.e., A sub-site admin only sees their content. My preference for this project would be django-cms, but I'm open to suggestions.
  5. Support for apps that pool the data from all the sub-sites, but limit sub-site "admins" to only viewing their records into that app.

Considering the above, what approach would you recommend? I am open to reconsidering technologies, but I would like to stick with Python.

Mcgrath answered 7/10, 2013 at 17:10 Comment(0)
H
13

There is a great app called django-tenant-schemas that uses PostgreSQL schemas mechanism to create multi-tenancy.

What you get is specyfing SHARED_APPS that contain objects shared across all the schemas (sub-sites), and TENANT_APPS that contain objects specific for a sub-site, i.e. users, records etc. The schemas are completely isolated from each other.

Each PostgreSQL schema is tied to a domain url, so that middleware checks the HOST part of the request and sets the db connection's schema to appriopriate one.

In addition, it allows you to define a PUBLIC_SCHEMA_URLCONF which allows you to specify urlconf file for public schema - the meta site that is not tied to any sub-site.

Harter answered 7/10, 2013 at 17:15 Comment(3)
Thanks for a quick answer! I looked into django-tenant-schemas, but the one thing that I got hung up on was that it doesn't seem to offer any help on the auth side of things (which I am not very experienced with). Would it be difficult to create a role or group that could be limited to specific sub-sites?Mcgrath
If you mean a user group or role that would make it possible for users in this group/role access defined sub-sites, then you would have to either make SiteUser models shared across all schemas (i.e. public one), that would contain, for each allowed schema, the id of the user model inside schema. Otherwise, you could just make the User model shared, and deal with authentication in another middleware/custom auth backend.Harter
Are there any security risks with django-tenant-schemas? Could a user view data outside of his schema?Sandfly
K
5

Sorry for quick and dirty answer, i just share what i've done to achieve multi tenancy:

  • django-tenancy I like the author's approach of using "dynamic model"
  • django-dynamicsite This is where dynamic SITE_ID based on domain will be linked to a tenant

Both libraries above, when combined, is able to serve a django instance which is multi-tenant, and flexible. What i mean flexible here is: you can define any model whether is it "tenant" or "global". So, you can have a site with global user but per tenant product catalogue, or per tenant + product. From many django app i've tried, this is the most flexible way to achieve multi tenancy

Klepht answered 25/11, 2013 at 2:55 Comment(0)
B
3

The Django based CMS Mezzanine also has multi-tenancy support.

It has most of the features you requested except the sub-site user controls I think. The admin page can be separated by site for admin users, but the normal users not.

However, if you dont need a CMS this might be an overkill for your use-case, But I wanted to mention it here for completeness.

Bering answered 4/2, 2015 at 18:2 Comment(0)
E
0

I have been trying to use django-tenants for a while along with Wagtail but this combination didn't work very well, or let me say, despite of a lot of try I was not able to get wagtail admin-page working correctly. I think will try to switch to django-tenant-schemas which I more widely used .

NOTE: django-tenant-schemas is not maintained now.

Edo answered 22/7, 2021 at 12:25 Comment(2)
django-tenant-schemas is no more maintained .... django-tenants is forked from it and is begin maintained actively... so I would not suggest to move to django-tenant-schemasEdo
I haven’t worked with Django since 2016, so this entire thread is pretty out of date at this point. Anyone reading this now should look at the “django-multitenant” project on github or at the “Multi-Tenancy in Django” blog post from viget.comMcgrath

© 2022 - 2024 — McMap. All rights reserved.