Skip to content

kdpisda/django-rls

Django RLS

PyPI version PyPI - Downloads CI Documentation codecov Python Version Django Version License

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

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.

About

Row Level Security for Django

Topics

Resources

License

Contributing

Security policy

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published

Contributors 3

  •  
  •  
  •