Skip to main content

PostgreSQL Row Level Security (RLS) for Django applications

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_rls-0.4.1.tar.gz (17.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_rls-0.4.1-py3-none-any.whl (21.3 kB view details)

Uploaded Python 3

File details

Details for the file django_rls-0.4.1.tar.gz.

File metadata

  • Download URL: django_rls-0.4.1.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

Hashes for django_rls-0.4.1.tar.gz
Algorithm Hash digest
SHA256 1a9bb2b6fee0d77f60c102eee0de2807bae8b01a0170319767298dfe06512d24
MD5 0b297d620ec3b38d1acd8f4de486aa53
BLAKE2b-256 e2cb8ecfc7aa7b6c5586204b6d07f1ba4d6b30e9f398ef7c5b6b0d1eefa9d378

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_rls-0.4.1.tar.gz:

Publisher: release.yml on kdpisda/django-rls

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file django_rls-0.4.1-py3-none-any.whl.

File metadata

  • Download URL: django_rls-0.4.1-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

Hashes for django_rls-0.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 290468995b7707f99bc8d48dc52147d865937cc07dce1cfebc33dce260fcf14a
MD5 a22d22d01f628b856afaa7539b0877c4
BLAKE2b-256 fff9c256e69c3ae8e7adceb4e0174f5e7019e41400f873a74574b7c81091ba1d

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_rls-0.4.1-py3-none-any.whl:

Publisher: release.yml on kdpisda/django-rls

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page