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

Contributing

Contributions are welcome! See the contributing guide to get started. This project is governed by the Contributor Covenant Code of Conduct.

License

MIT

Project details


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.3.0.tar.gz (128.9 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.3.0-py3-none-any.whl (68.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: django_rls_tenants-1.3.0.tar.gz
  • Upload date:
  • Size: 128.9 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.3.0.tar.gz
Algorithm Hash digest
SHA256 fc4870eded2cd1f2301d858a0cd9fbb796d0a41b1e5f9637b30d0a5c9181fdaf
MD5 bfba2ecfa9f5f69f0002a61cfade2ea0
BLAKE2b-256 c5cb833a8e62bba3698e88a8bc8c0a0b2f85e358163e9dbfca64b87e6a4d70a1

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_rls_tenants-1.3.0.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.3.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_rls_tenants-1.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 da10f3deef443cf80691789808fb2f24ce28cc120f5535f66964e7fc7fc165a2
MD5 aef6738b3a50fb4634711cb93ee51d9f
BLAKE2b-256 c5e200eb7b417111816e1dff889c7d88a7b53e00166da84d4f0580dcae002f77

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_rls_tenants-1.3.0-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