Skip to main content

Database-enforced multitenancy for Django using PostgreSQL Row-Level Security.

Project description

django-rls-tenants

CI PyPI version Python versions Django versions Code style: Ruff Type checked: mypy License: MIT

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",  # required
}

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

See the full comparison →

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

MIT

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_rls_tenants-1.2.2.tar.gz (90.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_rls_tenants-1.2.2-py3-none-any.whl (48.9 kB view details)

Uploaded Python 3

File details

Details for the file django_rls_tenants-1.2.2.tar.gz.

File metadata

  • Download URL: django_rls_tenants-1.2.2.tar.gz
  • Upload date:
  • Size: 90.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for django_rls_tenants-1.2.2.tar.gz
Algorithm Hash digest
SHA256 41d684a1db2164f50466bea08175a9d1f666502a4c6ebc8a9568febae506dd0b
MD5 78bb160a6fc0816a9e8dc9e9cde96ffb
BLAKE2b-256 25860e558ba3684aaeaddee9a26779b2918a2198c63f3594af281a357d5ab8cc

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_rls_tenants-1.2.2.tar.gz:

Publisher: publish.yml on dvoraj75/django-rls-tenants

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file django_rls_tenants-1.2.2-py3-none-any.whl.

File metadata

File hashes

Hashes for django_rls_tenants-1.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 a8f22b94134ec230c86388232b3f9eb2487e838b03cb31db185ddaa4b95f53dd
MD5 4317b7f39a83ea53def82af2c25dd106
BLAKE2b-256 3e729567bf4f839b7b4fe534e4bf2e9a59badead40797b9b4ebdc4638ad157b5

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_rls_tenants-1.2.2-py3-none-any.whl:

Publisher: publish.yml on dvoraj75/django-rls-tenants

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page