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.1.tar.gz (84.7 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.1-py3-none-any.whl (46.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: django_rls_tenants-1.2.1.tar.gz
  • Upload date:
  • Size: 84.7 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.1.tar.gz
Algorithm Hash digest
SHA256 9bc70ae1677a32faaffc23f4e94e569d84d38a0f6904330ce144327c2e08e70d
MD5 9b177e307fbc32c4c4e0a46afa7f31a7
BLAKE2b-256 fe65a5f29b817ade4fc157351904f9645e2f0e3a25c9bdd3ab4b9b8c7265fbde

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for django_rls_tenants-1.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b3d28e4961a3c156e78f9450ca5c35ec5484ee8d1390278567f06e4255f4fdb3
MD5 dec91f5a10ef1b0df3d2119da44b8cd3
BLAKE2b-256 9acfc92ea54266c47366759b2cbdf87235540ac85c7ff3ac1ee269d5b386addd

See more details on using hashes here.

Provenance

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