https://github.com/kdpisda/django-rls
Row Level Security for Django
https://github.com/kdpisda/django-rls
django postgres postgresql rls security
Last synced: 2 months ago
JSON representation
Row Level Security for Django
- Host: GitHub
- URL: https://github.com/kdpisda/django-rls
- Owner: kdpisda
- License: bsd-3-clause
- Created: 2025-06-29T03:46:58.000Z (12 months ago)
- Default Branch: main
- Last Pushed: 2026-01-05T10:45:14.000Z (6 months ago)
- Last Synced: 2026-01-06T16:54:58.731Z (6 months ago)
- Topics: django, postgres, postgresql, rls, security
- Language: Python
- Homepage: https://django-rls.com/
- Size: 264 KB
- Stars: 72
- Watchers: 4
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: .github/CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE
- Security: .github/SECURITY.md
Awesome Lists containing this project
README
# Django RLS
[](https://badge.fury.io/py/django-rls)
[](https://pypi.org/project/django-rls/)
[](https://github.com/kdpisda/django-rls/actions/workflows/ci.yml)
[](https://django-rls.com)
[](https://codecov.io/gh/kdpisda/django-rls)
[](https://pypi.org/project/django-rls/)
[](https://www.djangoproject.com/)
[](LICENSE)
A Django package that provides PostgreSQL Row Level Security (RLS) capabilities at the database level.
See [CONTRIBUTING.md](.github/CONTRIBUTING.md).
## Security
See [SECURITY.md](.github/SECURITY.md).
## Features
- ๐ Database-level Row Level Security using PostgreSQL RLS
- ๐ข Tenant-based and user-based policies
- ๐ **Pythonic Policies**: Define policies using standard Django `Q` objects
- ๐ณ **Hierarchical RLS**: Support for recursive CTEs and nested organizations
- โก **Context Processors**: Inject dynamic context variables (e.g. user IP, session data)
- ๐ง Django 5.0, 5.1, 5.2 (LTS), and 6.0 support
- ๐งช Comprehensive test coverage
- ๐ Extensible policy system
- โก Performance optimized
## Quick Start
```python
from django.db import models
from django.db.models import Q
from django_rls.models import RLSModel
from django_rls.policies import ModelPolicy, RLS
class Project(RLSModel):
name = models.CharField(max_length=100)
owner = models.ForeignKey(User, on_delete=models.CASCADE)
is_public = models.BooleanField(default=False)
class Meta:
rls_policies = [
# Pythonic Policy: Owner OR Public
ModelPolicy(
'access_policy',
filters=Q(owner=RLS.user_id()) | Q(is_public=True)
),
]
```
## Installation
Install from PyPI:
```bash
pip install django-rls
```
Or install the latest development version:
```bash
pip install git+https://github.com/kdpisda/django-rls.git
```
### Requirements
- Python 3.10, 3.11, 3.12, or 3.13
- Django 5.0, 5.1, 5.2 (LTS), or 6.0
- PostgreSQL 12 or higher (tested with PostgreSQL 17)
Add to your Django settings:
```python
INSTALLED_APPS = [
# ... your apps
'django_rls',
]
MIDDLEWARE = [
# ... your middleware
'django_rls.middleware.RLSContextMiddleware',
]
```
## Documentation
Full documentation is available at [django-rls.com](https://django-rls.com)
### Quick Links
- [Getting Started](https://django-rls.com/docs/intro)
- [Installation Guide](https://django-rls.com/docs/installation)
- [API Reference](https://django-rls.com/docs/api-reference)
- [Examples](https://django-rls.com/docs/examples/basic-usage)
## License
BSD 3-Clause License - see LICENSE file for details.