A Django package that provides PostgreSQL Row Level Security (RLS) capabilities at the database level.
- 🔒 Database-level Row Level Security using PostgreSQL RLS
- 🏢 Tenant-based and user-based policies
- 🔧 Django 5.0, 5.1, and 5.2 (LTS) support
- 🧪 Comprehensive test coverage
- 📖 Extensible policy system
- ⚡ Performance optimized
from django_rls.models import RLSModel from django_rls.policies import TenantPolicy, UserPolicy class TenantAwareModel(RLSModel): name = models.CharField(max_length=100) tenant = models.ForeignKey(Tenant, on_delete=models.CASCADE) class Meta: rls_policies = [ TenantPolicy('tenant_policy', tenant_field='tenant'), ]Install from PyPI:
pip install django-rlsOr install the latest development version:
pip install git+https://github.com/kdpisda/django-rls.git- Python 3.10, 3.11, 3.12, or 3.13
- Django 5.0, 5.1, or 5.2 (LTS)
- PostgreSQL 12 or higher (tested with PostgreSQL 17)
Add to your Django settings:
INSTALLED_APPS = [ # ... your apps 'django_rls', ] MIDDLEWARE = [ # ... your middleware 'django_rls.middleware.RLSContextMiddleware', ]Full documentation is available at django-rls.com
BSD 3-Clause License - see LICENSE file for details.