Database-enforced multitenancy for Django using PostgreSQL Row-Level Security.
Project description
django-rls-tenants
Database-enforced multitenancy for Django using PostgreSQL
Row-Level Security.
Every query -- ORM, raw SQL, dbshell -- is filtered by the database itself.
Missing tenant context returns zero rows, never leaks data.
Why this library?
Existing Django multitenancy libraries enforce isolation at the application
level — one missed filter and data leaks across tenants. django-rls-tenants
pushes enforcement to PostgreSQL itself using Row-Level Security. Every query —
ORM, raw SQL, dbshell — is filtered by the database. Missing tenant context
returns zero rows, never leaked data.
Learn more about the RLS approach →
Quick Start
pip install django-rls-tenants
# settings.py
INSTALLED_APPS = [..."django_rls_tenants"]
RLS_TENANTS = {
"TENANT_MODEL": "myapp.Tenant",
"TENANT_FK_FIELD": "tenant",
"GUC_PREFIX": "rls",
"USER_PARAM_NAME": "as_user",
"TENANT_PK_TYPE": "int",
"USE_LOCAL_SET": False,
"DATABASES": ["default"],
"STRICT_MODE": False,
}
MIDDLEWARE = [..."django_rls_tenants.RLSTenantMiddleware"]
# models.py
from django_rls_tenants import RLSProtectedModel
class Order(RLSProtectedModel):
title = models.CharField(max_length=255)
# tenant FK + RLS policy added automatically
python manage.py migrate
python manage.py check_rls # verify policies are in place
Full tutorial and documentation →
Want to see it in action first? Check out the example project --
cd example && docker compose up, then open http://localhost:8000.
Comparison with Alternatives
| Feature | django-rls-tenants | django-tenants | django-multitenant |
|---|---|---|---|
| Isolation mechanism | RLS policies | Separate schemas | ORM rewriting |
| Raw SQL protected | Yes | Yes (schemas) | No |
| Single schema | Yes | No | Yes |
| No connection routing | Yes | No | Depends |
| Fail-closed on missing ctx | Yes | N/A | No |
| Works with any API layer | Yes | Yes | Yes |
Used in Production
Are you using django-rls-tenants in production? We'd love to hear about it.
Open a discussion →
Requirements
| Dependency | Version |
|---|---|
| Python | >= 3.11 |
| Django | >= 4.2 |
| PostgreSQL | >= 15 |
License
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django_rls_tenants-1.2.0.tar.gz.
File metadata
- Download URL: django_rls_tenants-1.2.0.tar.gz
- Upload date:
- Size: 79.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
735169c517f37eae1f086cc85c7ecc3e74d61ba7af1275c23d31bb375b34cd8e
|
|
| MD5 |
575c13c3d63cec9007c9bf6e7fd9215c
|
|
| BLAKE2b-256 |
f1c27b0cb245df3065468a57d036d6d7118558149e58c3b9c200c8b2b87e086b
|
Provenance
The following attestation bundles were made for django_rls_tenants-1.2.0.tar.gz:
Publisher:
publish.yml on dvoraj75/django-rls-tenants
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
django_rls_tenants-1.2.0.tar.gz -
Subject digest:
735169c517f37eae1f086cc85c7ecc3e74d61ba7af1275c23d31bb375b34cd8e - Sigstore transparency entry: 1154358571
- Sigstore integration time:
-
Permalink:
dvoraj75/django-rls-tenants@4b8e7c53cd3da430403a89dd5a5429fe9e381cf8 -
Branch / Tag:
refs/tags/v1.2.0 - Owner: https://github.com/dvoraj75
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@4b8e7c53cd3da430403a89dd5a5429fe9e381cf8 -
Trigger Event:
release
-
Statement type:
File details
Details for the file django_rls_tenants-1.2.0-py3-none-any.whl.
File metadata
- Download URL: django_rls_tenants-1.2.0-py3-none-any.whl
- Upload date:
- Size: 45.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
da647bc1174629e110cb357dc81b9de915e7c256fe5a9156bbbe8d7f2c4e0636
|
|
| MD5 |
5db4d272ffaf46b25bc35f80b0636b65
|
|
| BLAKE2b-256 |
29194d7c3e4be0c57cda583a0119e6455b617cc579defd21f8c458187d2acb64
|
Provenance
The following attestation bundles were made for django_rls_tenants-1.2.0-py3-none-any.whl:
Publisher:
publish.yml on dvoraj75/django-rls-tenants
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
django_rls_tenants-1.2.0-py3-none-any.whl -
Subject digest:
da647bc1174629e110cb357dc81b9de915e7c256fe5a9156bbbe8d7f2c4e0636 - Sigstore transparency entry: 1154358573
- Sigstore integration time:
-
Permalink:
dvoraj75/django-rls-tenants@4b8e7c53cd3da430403a89dd5a5429fe9e381cf8 -
Branch / Tag:
refs/tags/v1.2.0 - Owner: https://github.com/dvoraj75
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@4b8e7c53cd3da430403a89dd5a5429fe9e381cf8 -
Trigger Event:
release
-
Statement type: