# Multitenancy in django

**URL:** <https://forum.djangoproject.com/t/multitenancy-in-django/41540>\
**Category:** Using Django\
**Created:** [June 23, 2025, 12:45pm UTC](https://forum.djangoproject.com/t/multitenancy-in-django/41540 "2025-06-23T12:45:04Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![davisilvarafacho](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/davisilvarafacho/32/21581_2.png) [@davisilvarafacho](https://forum.djangoproject.com/u/davisilvarafacho)\
**Post date:** [June 23, 2025, 12:45pm UTC](https://forum.djangoproject.com/t/multitenancy-in-django/41540/1 "2025-06-23T12:45:04Z")

</div>

Hi everyone,

I’m from Brazil, So I apologize in advance for any English mistakes.

Someone that uses django in production as an SaaS can explain to me how you are doing your multitenancy?

I’m a pleno dev and I doesn’t have any idea of the ideal way of do this. In the company where I work, we have one database per client, which every JWT token has the database name, so we get this in the middleware and change the **DATABASE\_NAME** in every request.

I’m also developing a lib called django-islands that implements a tenant\_id column and in every database query an WHERE clause is added in the end of the query.

This options above are good? I’m creating this card more because I don’t know of any library that implements the multitenancy process, even though it is an extremely common and necessary process.

---

<div class="post-metadata">

**Author:** ![boxed](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/boxed/32/11975_2.png) [@boxed](https://forum.djangoproject.com/u/boxed)\
**Post date:** [June 24, 2025, 12:50pm UTC](https://forum.djangoproject.com/t/multitenancy-in-django/41540/2 "2025-06-24T12:50:08Z")

</div>

Data that is unique per customer has a foreign key so you can know which customer it belongs to. Then you filter on that fk everywhere.

I think the reason there aren’t many libraries is that it’s a common pattern, but the details will depend on the business rules. In other words: general systems will not work for most businesses as they have specific needs.

---

<div class="post-metadata">

**Author:** ![leandrodesouzadev](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/leandrodesouzadev/32/8981_2.png) [@leandrodesouzadev](https://forum.djangoproject.com/u/leandrodesouzadev)\
**Post date:** [June 25, 2025, 11:02am UTC](https://forum.djangoproject.com/t/multitenancy-in-django/41540/3 "2025-06-25T11:02:02Z")

</div>

Hello there!  
Greetings from Brazil as well!  
There are a lot of known approaches for multi-tenancy. I’ve never used this separated-database approach before, but it makes sense in some scenarios.

> [@davisilvarafacho](#):
>
> In the company where I work, we have one database per client, which every JWT token has the database name, so we get this in the middleware and change the **DATABASE\_NAME** in every request.

So this seems to be working already?

I’m just curious about this sentence.

> [@davisilvarafacho](#):
>
> I’m creating this card more because I don’t know of any library that implements the multitenancy process

I just digged into the source code of the `django-islands` repository, and it looks a lot like the code from [django-multitenant](https://github.com/citusdata/django-multitenant) package (their approach is to use the same database with tenant\_id column). Just remember to follow the MIT license if you’re copying the source code.

You can also take notes from other approaches from packages like [django-tenants](https://django-tenants.readthedocs.io/) that uses schema-based tenancy.

I’ve used `django-tenants` in the past, and I don’t like it because it’s hard to get into “migration hell”, where migrations takes minutes to process on all schemas.  
Since past year I’ve been working with `django-multitenant` and having good results with almost 1000 tenants.  
Feel free to reach out if you any further questions.

---

<div class="post-metadata">

**Author:** ![davisilvarafacho](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/davisilvarafacho/32/21581_2.png) [@davisilvarafacho](https://forum.djangoproject.com/u/davisilvarafacho)\
**Post date:** [June 25, 2025, 3:18pm UTC](https://forum.djangoproject.com/t/multitenancy-in-django/41540/4 "2025-06-25T15:18:46Z")

</div>

I understand that it is more a question of the business than the software, but the separation of tenant data is almost as essential as the database itself, since there is no SaaS without tenant separation.

I wanted to know if I am out of touch with what the market is using or if this type of approach/solution really does not exist.

---

<div class="post-metadata">

**Author:** ![davisilvarafacho](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/davisilvarafacho/32/21581_2.png) [@davisilvarafacho](https://forum.djangoproject.com/u/davisilvarafacho)\
**Post date:** [June 25, 2025, 3:36pm UTC](https://forum.djangoproject.com/t/multitenancy-in-django/41540/5 "2025-06-25T15:36:27Z")

</div>

Hi @leandrodesouzadev, how you doing?  
I’m speaking from Belo Horizonte - MG, what about you?

> [@leandrodesouzadev](#):
>
> So this seems to be working already?

This approach of tenants by banks has been around for a few years now! We like the idea of ​​physical isolation mainly because of the LGPD.

> [@leandrodesouzadev](#):
>
> Just remember to follow the MIT license if you’re copying the source code.

I am following the same license. I created a separate repository and not a fork because I wanted something focused on multi-tenancy and not on citus compatibility, apart from other features that I am developing such as “tenant levels”, where there is a main tenant, but there are other entities that behave as a data access limiter.

> [@leandrodesouzadev](#):
>
> Since past year I’ve been working with `django-multitenant` and having good results with almost 1000 tenants.

We had some issues with the django-multitenant code and we missed proactiveness from the dev team fixing some bugs. That’s why i created django-islands.

I already knew django-tenants, but the “migration hell” is what we live today, sometimes we have 20 migrations to all tenants. Sometimes one specific migrations goes wrong in one specific database and we have to stop and discovery what goes wrong. Sometimes it’s very chaotic, but works well, even the cost of this is very high.

---

<div class="post-metadata">

**Author:** ![leandrodesouzadev](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/leandrodesouzadev/32/8981_2.png) [@leandrodesouzadev](https://forum.djangoproject.com/u/leandrodesouzadev)\
**Post date:** [June 25, 2025, 4:18pm UTC](https://forum.djangoproject.com/t/multitenancy-in-django/41540/6 "2025-06-25T16:18:51Z")

</div>

> [@davisilvarafacho](#):
>
> I’m speaking from Belo Horizonte - MG, what about you?

I’m from the South, on Santa Catarina.

> [@davisilvarafacho](#):
>
> We had some issues with the django-multitenant code and we missed proactiveness from the dev team fixing some bugs. That’s why i created django-islands.

What specific issues are you facing? We have a few helper functions and custom models that does most of the work we need, and to support a few things that the built-in functions “kinda” support like setting more than one tenant.

---

<div class="post-metadata">

**Author:** ![davisilvarafacho](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/davisilvarafacho/32/21581_2.png) [@davisilvarafacho](https://forum.djangoproject.com/u/davisilvarafacho)\
**Post date:** [June 26, 2025, 6:26pm UTC](https://forum.djangoproject.com/t/multitenancy-in-django/41540/7 "2025-06-26T18:26:30Z")

</div>

I love the South! I’ve visited Gramado once, a beautiful city!

The errors weren’t serious at all, they were related to a configuration error in our code with the tool. For example: linking a tenantforegeinkey to a model that didn’t inherit from tenantmodel. Some silly configuration errors on our part, plus the lack of clear documentation, made us feel insecure and doubtful about the library.

All of this, combined with the fact that they hadn’t released a version in over a year, made it seem like the project was broken and abandoned. That’s why I created django-islands, to fix what was wrong and have confidence that if something broke, I could fix it.
