17

Are there common patterns people use for creating multi-tenanted applications using Django. The built in "sites" framework seems like an option. Are there other approaches people have had success with?

hlovdal
  • 26,565
  • 10
  • 94
  • 165
  • 1
    possible duplicate of [optimal architecture for multitenant application on django](http://stackoverflow.com/questions/7194341/optimal-architecture-for-multitenant-application-on-django) – akaihola Jan 31 '13 at 10:24

2 Answers2

10

Using the sites framework goes a long way towards providing a security guarantee to the "tenants", assuming you give each site instance a unique table.

On the other hand, it'll be a big hassle if you have a small number of tenants and you'll waste a huge amount of server resources since you'll need at least one server process per customer even if they aren't using the system. If you have a large number of tenants it won't be quite as much hassle because you'll be forced to automate the solution regardless of your approach.

Putting a tenant foreign key in almost all your models will work just fine and Django's ORM makes it easy (easier?) to enforce security using custom managers. The drawback is performance if you start getting hammered with lots of users, because there's no easy way to scale up.

If you do need to scale, I think the best solution could be a combination of both approaches. Every model has a tenant foreignkey so databases can be shared but then you develop some mechanism at a higher level than Django to route customers into a site instance. This lets you put really big tenants on their own databases with resources properly tuned just for them (e.g. the proper number of mod_wsgi daemons, number of database connections, memcache pool properly sized, etc.) and smaller tenants share common resources.

Van Gale
  • 43,536
  • 9
  • 71
  • 81
7

Take a look at https://github.com/bcarneiro/django-tenant-schemas You'll only have one project instance and won't have to do many modifications in your code.

Clash
  • 4,896
  • 11
  • 47
  • 67
  • This is a nice approach if you want multiple DBs - lets you use multiple schemas in a single DB, for less overhead. Requires PostgreSQL. Django simple-tenant-schemas is also interesting. – RichVel Dec 13 '12 at 17:28
  • 1
    I meant "if you want multiple DB-like approach but using schemas", sorry – RichVel Dec 14 '12 at 21:35
  • 2
    Just wanted to +1 this answer as I have recently forked this project for a multi-tenant schema app and it is great! Well written, easily extensible, and nearly hassle free once implemented. – HurnsMobile Jan 12 '13 at 18:02