PostgreSQL Row Level Security (RLS) for Django applications
Project description
Django RLS
A Django package that provides PostgreSQL Row Level Security (RLS) capabilities at the database level.
See CONTRIBUTING.md.
Security
See SECURITY.md.
Features
- 🔒 Database-level Row Level Security using PostgreSQL RLS
- 🏢 Tenant-based and user-based policies
- 🐍 Pythonic Policies: Define policies using standard Django
Qobjects - 🌳 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
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:
pip install django-rls
Or install the latest development version:
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:
INSTALLED_APPS = [
# ... your apps
'django_rls',
]
MIDDLEWARE = [
# ... your middleware
'django_rls.middleware.RLSContextMiddleware',
]
Documentation
Full documentation is available at django-rls.com
Quick Links
License
BSD 3-Clause License - see LICENSE file for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django_rls-0.4.0.tar.gz.
File metadata
- Download URL: django_rls-0.4.0.tar.gz
- Upload date:
- Size: 17.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f20c653e74c8207f3cceeb5f9fe1e76e0ab96a6d69b7076b6505ba60a36b5203
|
|
| MD5 |
9c1adafd09a076a858903fe24626ff46
|
|
| BLAKE2b-256 |
0d8239a57ed093ee41487b59a1755a34699e843eb101a84526e92b3729b2fedb
|
Provenance
The following attestation bundles were made for django_rls-0.4.0.tar.gz:
Publisher:
release.yml on kdpisda/django-rls
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
django_rls-0.4.0.tar.gz -
Subject digest:
f20c653e74c8207f3cceeb5f9fe1e76e0ab96a6d69b7076b6505ba60a36b5203 - Sigstore transparency entry: 787285897
- Sigstore integration time:
-
Permalink:
kdpisda/django-rls@04ab4a3373180849807b98e6a357cb85571c4f6b -
Branch / Tag:
refs/heads/main - Owner: https://github.com/kdpisda
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@04ab4a3373180849807b98e6a357cb85571c4f6b -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file django_rls-0.4.0-py3-none-any.whl.
File metadata
- Download URL: django_rls-0.4.0-py3-none-any.whl
- Upload date:
- Size: 21.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ecb1bd9158cc23c48ba77b64688fcd2c3d42b1fb87fddd3a5cb5619dd80823cf
|
|
| MD5 |
a113aaacb2b988f818655c9431f7e06e
|
|
| BLAKE2b-256 |
49819eeca21ad737e08eaf490ebada6e5fe9df8cf7e5430bf0998152e4578164
|
Provenance
The following attestation bundles were made for django_rls-0.4.0-py3-none-any.whl:
Publisher:
release.yml on kdpisda/django-rls
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
django_rls-0.4.0-py3-none-any.whl -
Subject digest:
ecb1bd9158cc23c48ba77b64688fcd2c3d42b1fb87fddd3a5cb5619dd80823cf - Sigstore transparency entry: 787285898
- Sigstore integration time:
-
Permalink:
kdpisda/django-rls@04ab4a3373180849807b98e6a357cb85571c4f6b -
Branch / Tag:
refs/heads/main - Owner: https://github.com/kdpisda
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@04ab4a3373180849807b98e6a357cb85571c4f6b -
Trigger Event:
workflow_dispatch
-
Statement type: