Key Features of Django RLS

Django RLS comes packed with features designed to make implementing Row Level Security in your Django applications straightforward and efficient. Let’s explore what makes Django RLS a comprehensive solution for database-level access control.

True Database-Level Row Level Security

The core feature of Django RLS is genuine database-level security:

  • RLS Policies: Policies are defined directly in PostgreSQL, not in your application
  • Transparent Filtering: Your Django querysets work normally, but PostgreSQL automatically applies filtering
  • No Bypass Possible: Even with direct database access, policies are enforced

Built-in Tenant-Based Policies

Django RLS includes ready-to-use policies for common scenarios:

from django_rls.policies import TenantPolicy

class Document(RLSModel):
    tenant = models.ForeignKey(Tenant, on_delete=models.CASCADE)
    
    class Meta:
        rls_policies = [
            TenantPolicy('tenant_isolation', tenant_field='tenant')
        ]

This single policy configuration ensures complete tenant isolation across all queries.

User-Based Access Control

Control data access at the individual user level:

from django_rls.policies import UserPolicy

class Document(RLSModel):
    owner = models.ForeignKey(User, on_delete=models.CASCADE)
    
    class Meta:
        rls_policies = [
            UserPolicy('owner_policy', user_field='owner')
        ]

Now users automatically see only their own documents.

Extensible Policy System

Need custom policies beyond the built-ins? Django RLS provides a flexible system:

from django_rls.policies import BasePolicy

class CustomPolicy(BasePolicy):
    def get_condition(self):
        return "some_column > current_setting('app.user_level')"

Define any PostgreSQL condition-based policy you need.

Automatic Context Management

Django RLS includes middleware that automatically manages context:

MIDDLEWARE = [
    'django_rls.middleware.RLSMiddleware',  # Automatically sets user context
]

No need to manually pass context around - it’s handled automatically from requests.

Comprehensive Test Utilities

Testing RLS policies is easy with built-in utilities:

from django_rls.test_utils import RLSTestCase

class DocumentTestCase(RLSTestCase):
    def test_user_isolation(self):
        # Automatically sets up user context for testing
        doc = Document.objects.create(owner=self.user)
        self.assertEqual(Document.objects.count(), 1)

Django ORM Integration

Django RLS integrates seamlessly with Django’s ORM:

  • QuerySet API: Use familiar .filter(), .exclude(), .get() methods
  • Aggregations: Works with .aggregate() and .annotate()
  • Transactions: Full transaction support
  • Migrations: Policies are managed through Django migrations

Admin Interface Support

Manage RLS-protected data through Django’s admin:

  • Automatic Filtering: Admin lists respect RLS policies
  • Safe by Default: You can’t accidentally view restricted data
  • Audit Trail: All access is logged by PostgreSQL

Django REST Framework Support

If you’re using DRF, Django RLS works seamlessly:

  • Serializers: Works with DRF serializers
  • Viewsets: Apply policies automatically to viewsets
  • Permissions: Compatible with DRF permission classes

Performance-Optimized

Django RLS is built with performance in mind:

  • Index-Aware: RLS conditions work with database indexes
  • Query Optimization: PostgreSQL optimizes queries with RLS policies
  • Minimal Overhead: Policies are evaluated at the database level

Flexible Policy Combinations

Combine multiple policies for complex scenarios:

class Document(RLSModel):
    tenant = models.ForeignKey(Tenant, on_delete=models.CASCADE)
    owner = models.ForeignKey(User, on_delete=models.CASCADE)
    
    class Meta:
        rls_policies = [
            TenantPolicy('tenant_isolation', tenant_field='tenant'),
            UserPolicy('owner_policy', user_field='owner'),
        ]

Both policies are applied, ensuring data isolation at multiple levels.

Summary

Django RLS provides a complete feature set for implementing database-level security in Django applications, from simple user isolation to complex multi-tenant scenarios with custom policies.