Skip to content

django-rls-tenants

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

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


Why Row-Level Security?

Most Django multitenancy libraries filter data in application code -- through ORM managers, middleware, or custom querysets. If a developer forgets a filter, or writes raw SQL, tenant data leaks silently.

django-rls-tenants pushes isolation into the database using PostgreSQL Row-Level Security policies. Every query -- ORM, raw SQL, dbshell, migration scripts -- is subject to the same policy, provided the application connects as a role without SUPERUSER or BYPASSRLS. The database itself becomes the trust boundary for forgotten filters and unscoped queries; see the Security Model for what that does and does not cover.

Key Features

  • Database-enforced isolation -- RLS policies apply to every query, not just ORM calls.
  • Automatic query scoping -- queries are filtered at both ORM and database levels for defense-in-depth.
  • Fail-closed by default -- missing tenant context returns zero rows, never leaks data.
  • Optional strict mode -- STRICT_MODE=True raises NoTenantContextError on unscoped queries, turning silent empty results into loud errors during development.
  • Single schema, single database -- no schema-per-tenant overhead.
  • API-agnostic -- works with GraphQL, async views, management commands, and (through the DRF integration) Django REST Framework.
  • M2M isolation -- auto-detected subquery-based RLS policies on M2M through tables; check_rls --generate writes the migration that creates them.
  • Third-party tables -- AddRLSPolicy adds the same policy to a table of an app you do not own, and RLS_TENANTS["EXTERNAL_MODELS"] makes check_rls, the cross-tenant foreign key check and the select_related() / prefetch_related() tenant filters treat the model as protected. See Tables You Do Not Own.
  • Sharing rows across tenants -- extra_predicates (SharedWith, TenantField, RawPredicate) let a tenant read rows another tenant shares with it, in a separate FOR SELECT policy that never lets it update or delete them; including_shared() queries them. See Sharing Rows Across Tenants.
  • Tenant-aware Django admin -- RLSTenantModelAdmin scopes changelists, forms, and related dropdowns to the current tenant, with a tenant switcher for cross-tenant admins. See the Admin guide.
  • Cross-tenant foreign key validation -- full_clean(), forms, the admin and DRF serializers reject a foreign key to another tenant's row, which RLS cannot catch; validate_cross_tenant_fks() covers service code. See Protecting Models.
  • Native Celery integration -- @rls_task carries the active tenant context from enqueue to the worker, across chains, groups, and chords. See the Celery Tasks guide.
  • Django REST Framework integration -- RLSTenantAPIViewMixin opens the RLS context after DRF authenticates the user (token and JWT clients included, which the middleware cannot see), RLSTenantPermission refuses a view that runs outside it, and RLSTenantModelSerializer fills the tenant FK from it. See the Django REST Framework guide.
  • Django Ninja integration -- RLSTenantNinjaAPI opens the RLS context after Ninja authenticates the user (bearer and API-key clients included, which the middleware cannot see), RLSTenantGuard refuses an operation that runs outside it, and RLSTenantModelSchema with rls_save() writes a payload in the active tenant. See the Django Ninja guide.
  • Per-tenant management commands -- run_as_tenant runs any management command inside one tenant's context and run_for_all_tenants runs it once per tenant, with filters, --parallel, --json output and a failure summary. See the Management Commands guide.
  • Audit logging -- structured records on the django_rls_tenants.audit logger each time a tenant or admin context is entered, exited or refused, routed with Django's LOGGING. See the Audit Logging guide.
  • Users in several tenants -- a user model that adds rls_tenant_ids (MultiTenantUser) selects the tenant a request runs in with an X-Tenant header or switch_tenant(); the middleware, the DRF mixin and the admin check every selection against the user's tenants and refuse any other with a 403. See the Users in Several Tenants guide.
  • Raw SQL helpers -- safe_tenant_sql() builds a tenant-filter predicate; current_tenant_value_sql() returns the current tenant ID expression for inserts and projections. Both reuse the expressions used by RLS policies. See the Raw SQL guide.
  • Clean internal layering -- generic rls/ primitives separate from tenants/ conveniences.
  • Configurable escape hatches -- bypass flags for authentication, admin, migrations.
  • Drop-in for new projects -- abstract model, middleware, and test helpers included.

I want to...

Goal Start here
Install and protect my first model Quick Start, then Protecting Models
Expose the tenant on my User model User Integration
Set the tenant context for every request Middleware
Let one user work in several tenants Users in Several Tenants
Set the context in a script or management command Context Managers
Run a management command as one tenant, or for every tenant run_as_tenant / run_for_all_tenants
Run a Celery task under the right tenant Celery Tasks
Scope a DRF API to the user's tenant Django REST Framework
Scope a Django Ninja API to the user's tenant Django Ninja
Use the tenant context in an async view or consumer Async Code
Use the Django admin across tenants Admin
Keep an audit trail of tenant context changes Audit Logging
Write a test that proves isolation fails closed Testing
Run my tests as a role RLS applies to (not a superuser) Enforcing RLS in Tests
Test async code under a tenant context Async Tests
Write raw SQL that stays tenant-scoped Raw SQL
Let authentication or migrations bypass RLS Bypass Mode
Run behind PgBouncer or pgpool-II, with Django's connection pool or with persistent connections Connection Pooling
Deploy on an ASGI server (Uvicorn, Daphne, Hypercorn) Async Deployment
Index tables so RLS stays fast Performance & Indexing
Verify the policies are really in the database check_rls
Migrate an existing app or another multitenancy library Migration Guide
Understand what RLS does and does not guarantee Security Model
Fix something that is wrong right now Troubleshooting

How It Works

┌──────────────┐    ┌──────────────┐    ┌──────────────┐
│   Request    │───▶│  Middleware  │───▶│  Set GUCs    │
│              │    │  resolves    │    │  on PG conn  │
│              │    │  tenant user │    │              │
└──────────────┘    └──────────────┘    └──────┬───────┘
                                               │
                                               ▼
                                       ┌──────────────┐
                                       │  PostgreSQL  │
                                       │  RLS Policy  │
                                       │  filters rows│
                                       └──────────────┘
  1. Middleware reads request.user and extracts tenant identity via the TenantUser protocol.
  2. GUC variables (rls.current_tenant, rls.is_admin) are set on the PostgreSQL connection.
  3. Auto-scoping adds WHERE tenant_id = X at the ORM level, enabling composite index usage (see the Performance & Indexing guide).
  4. RLS policies (created automatically via Django migrations) filter every query at the database level as a safety net.
  5. Fail-closed: if no GUC is set, the policy returns zero rows -- no data leak is possible.

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

Requirements

Dependency Version
Python 3.12, 3.13, 3.14
Django 5.2, 6.0, 6.1
PostgreSQL 15, 16, 17, 18

See the supported-versions policy for how these lists change.

Quick Start

pip install django-rls-tenants
# settings.py
INSTALLED_APPS = [
    # ...
    "django_rls_tenants",
]

RLS_TENANTS = {
    "TENANT_MODEL": "myapp.Tenant",
}

MIDDLEWARE = [
    # ...
    "django_rls_tenants.RLSTenantMiddleware",
]

See the full Quick Start tutorial for a complete walkthrough.

Try the demo

See django-rls-tenants in action with the example project. cd example && docker compose up, and explore tenant isolation in under 5 minutes.

Community

Used in production? Tell us in the discussion: which deployment shapes and features people run decides what gets built next.

Questions, bug reports and security reports each have their own place; see SUPPORT.md.

License

MIT