Hi Guys
Need guidance to know how to design / develop the Django multi-tenant application. We are using Python, and SQL lite database, but willing to use PostgreSQL or mySQL database if required.
Following is the scenario.
1- We have got the domain application.testcom
1- Users use their own application url i.e. user1.applicaiton.testcom , user2.application.testcom , user3.application.testcom
2- We need to keep the data specific to the user1, user2 and user3 etc
3- once the user login to the application, the login page should show the logo and application name specific to the users e.g. “User1 App”, “User2 App”, “User3 App”
4- We are using one database schema for application and want to use the application logic to achieve the above.
5- Do we need to create separate database schema for each user like user1_db_schema, user2_db_schema, user3_db_schema to achieve the above 3 points?
Thanks in advance 
Hello there.
You have a few options, actually there are several approaches for this kind of problem.
If you want all the work done for you for routing based on the domain, then django-tenants is the answer you’re looking for. Key notes about it:
- Tenant data are separated by schemas (I think it only works on PSQL) and each tenant has it’s own schema. This come with a few drawbacks though, specially migrations, I’ve worked on a few projects and the migrations can take from minutes to hours to complete, depending on the number of tenants you have.
- It uses a subdomain to route the data to a specific tenant., and set the schema accordingly.
If you want a bit more control over the solution, and all data on a single database and schema, you can use django-multitenant, it doesn’t had a release in a few years, but it’s quite stable. I’ve been using for the last few years, and it’s a setup and forget, doesn’t cause any issues. We need to create a few wrappers around some of the functions to get the behavior we wanted for a tenant hierarchy. You won’t have all the routing done for you, you’ll need to it yourself, but you’ll find that the code for this is quite straight-forward, you can find some inspiration from the django-tenants middlewares on how to do that. The experience moving from django-tenants to django-multitenant was amazing, didn’t have any problems with the database since. And if you project scale to the moon, you can shard you database with their solution (never needed that). Right now we’re running a project with about 1k users and tenants.
There are probably a few other options that you might want to consider, based on your use case. I can’t provide such advice on these others, since I didn’t use them.
I hope you find this useful.
Hi,
I’m having the same issue. In my case, i’m thinking about using multiple SQLite databases and use a database router for each tenant. This is a viable option, but probably not the most efficient one. The biggest win here, is that the migrations can be easier and totally isolated by tenant and you can make it in canary way if you want (but it can difficult), and as the database is just a SQLite file replication is easy enough. I see that SQLite is more than enough for the vast majority of use cases.
Short answer to your question 5: no, you don’t need a separate schema for each user. One database with a tenant field on your data works fine for this, and it’s how most Django SaaS apps start out.
What I’d do is create a Tenant model that stores the subdomain, the app name, and the logo. Then write a small middleware that grabs the subdomain from the request, finds the matching tenant, and sets it as request.tenant (and returns a 404 if nothing matches). Because this happens before login, your login page can already show “User1 App” with the right logo. Throw it in a context processor, and it’s available on every page too.
For the data itself, put a tenant foreign key on every model that holds customer data, and have it filtered automatically, either with a custom model manager or django-multitenant as Leandro mentioned. Relying on remembering .filter(tenant=...) in every view is how leaks happen. Also, check at login that the user actually belongs to that tenant, otherwise, someone from user1 could log in on user2’s subdomain. We learned that one the hard way on a multi-tenant project at Technource, and it’s easy to miss in testing because everything looks fine until two tenants are using it at once.
On the setup side, add .application.test.com with the leading dot to ALLOWED_HOSTS, and get a wildcard DNS record and an SSL certificate so new tenants just work. And honestly, I’d switch to PostgreSQL before you launch. SQLite is great while developing, but it locks the whole database on every write, and that starts to hurt once a few tenants are using the app at the same time.
For your setup, I would start by deciding how much isolation you really need before choosing between a shared schema and separate schemas. A single shared schema with a tenant column on each table is the simplest to run, and it works well when tenants are small and you are comfortable enforcing the tenant filter in every query, usually through middleware that sets the current tenant and a custom manager. Separate PostgreSQL schemas, as django-tenants does, give stronger isolation and make per-tenant backups easier, but migrations and background jobs get more complex. Since you also need per-tenant branding on the login page, store the logo and application name against the tenant record and resolve the tenant from the subdomain before rendering the page. I would move to PostgreSQL rather than SQLite for this, because schema support and concurrent writes matter once several tenants share the same deployment.