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",
    "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

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.0.tar.gz (79.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.0-py3-none-any.whl (45.6 kB view details)

Uploaded Python 3

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

Hashes for django_rls_tenants-1.2.0.tar.gz
Algorithm Hash digest
SHA256 735169c517f37eae1f086cc85c7ecc3e74d61ba7af1275c23d31bb375b34cd8e
MD5 575c13c3d63cec9007c9bf6e7fd9215c
BLAKE2b-256 f1c27b0cb245df3065468a57d036d6d7118558149e58c3b9c200c8b2b87e086b

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for django_rls_tenants-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 da647bc1174629e110cb357dc81b9de915e7c256fe5a9156bbbe8d7f2c4e0636
MD5 5db4d272ffaf46b25bc35f80b0636b65
BLAKE2b-256 29194d7c3e4be0c57cda583a0119e6455b617cc579defd21f8c458187d2acb64

See more details on using hashes here.

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

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