

Show HN: django-db-multitenant: multiple tenants in a single Django app server - mik3y
https://github.com/mik3y/django-db-multitenant

======
mik3y
Little more background for the HN Django crowd:

This was inspired from an older HN discussion here:
[https://news.ycombinator.com/item?id=4270003](https://news.ycombinator.com/item?id=4270003)

I wanted to take a Django app not designed for multitenancy and serve several
tenants from a single Django gunicorn instance. Scaling to support more
tenants beyond the server's capacity is just a matter of turning up a new
appserver (from a baked AMI in my case).

It works with MySQL by issuing a new ``USE`` statement whenever the current
request's resolved tenant (database) does not match the connection.

Tenant mapping is abstracted into a method that takes a request and returns
the database name. In my case, that's a lookup against a Redis server using
the request's hostname.

It's a bit devious (as noted in the README!) but works well for my limited
application. Perhaps someone else will find it useful.

