A comprehensive, production-ready prompt for implementing row-level tenant isolation in Django projects. Think of this as pip install multi-tenant for your architecture - copy the implementation prompt, paste it into your AI assistant, and get guided step-by-step through the entire implementation customized to your project.
This is like pip install but fully configured for architecture patterns - copy and go!
- Step 1: Copy contents of this repository to your Django project folder
- Step 2: Open implementation-prompt.md and copy its contents
- Step 3: Paste into your AI assistant (ChatGPT, Claude, GitHub Copilot Chat, etc.)
The AI will guide you through the complete implementation, step-by-step, customized to your project.
Row-level tenancy (also called single-database multi-tenancy) is an architecture pattern where multiple organizations (tenants) share the same database and tables, but each row is tagged with a tenant identifier. This means:
- One database, many tenants - All organizations share the same PostgreSQL database
- Data isolation per row - Every record has a
tenant_idforeign key that isolates data - Automatic filtering - Queries automatically filter by tenant, preventing data leaks
- Shared infrastructure - More cost-effective than separate databases per tenant
| Approach | Best For | Pros | Cons |
|---|---|---|---|
| Row-Level (This Guide) | SaaS with 100s-1000s of tenants | Cost-effective, easy scaling, shared resources | Requires careful query filtering |
| Schema-per-Tenant | 10-50 tenants with compliance needs | Strong isolation, easier migrations | Complex management, higher costs |
| Database-per-Tenant | <10 large enterprise clients | Maximum isolation, custom configurations | Expensive, difficult to scale |
Row-level tenancy is the industry standard for modern SaaS applications like Slack, GitHub, Shopify, and most B2B platforms.
Traditional implementation takes 2-4 weeks and requires deep Django expertise. This prompt-based approach:
- 30-120 minutes instead of 2-4 weeks - Pre-planned architecture and battle-tested patterns
- AI-guided implementation - Get unstuck instantly with context-aware guidance
- Copy-paste code examples - Working code blocks, not just concepts
- Proven patterns - Based on production implementations serving thousands of tenants
- Built-in testing strategy - Comprehensive test coverage prevents data leaks
- Migration safety - Step-by-step data migration with rollback procedures
- Free and open-source - No consultant fees ($10k-50k savings)
- Reduced debugging time - Common pitfalls already documented
- Reusable across projects - Use this pattern for every Django SaaS you build
- Production-ready - Includes security, performance, and compliance considerations
- Role-based access control - Four-tier permission system included
- Performance optimized - Database indexing strategy included
Bottom line: What normally requires hiring a consultant or spending weeks researching is condensed into a simple prompt you paste into ChatGPT/Claude.
- π Complete Data Isolation: Row-level tenant separation with automatic query filtering
- π₯ Role-Based Access Control: Four-tier permission system (Owner/Admin/Member/Viewer)
- π Backward Compatible: Add to existing projects without breaking functionality
- π Production Ready: Based on proven patterns with comprehensive testing strategies
- π Easy Integration: Clear phase-by-phase implementation guide
- π Extensive Documentation: 9 detailed guides covering every aspect
Alternative: Use the Quick Reference if you prefer to implement manually with a cheat sheet.
This repository contains a complete implementation plan with:
- Overview & Architecture - High-level design and scope
- Architecture & Design - Detailed system architecture
- Requirements - Functional and non-functional requirements
- Phase 1: Foundation - Core models and managers
- Phase 2: Middleware & Access - Middleware and decorators
- Phase 3: Management & Utilities - Admin and utilities
- Testing Strategy - Unit, integration, and performance tests
- Risks & Success Criteria - Risk management and metrics
- Code Examples & Configuration - Complete working examples
| Phase | Duration | Complexity | Dependencies |
|---|---|---|---|
| Phase 1: Foundation | 2-3 minutes | Medium | Database migration planning |
| Phase 2: Middleware & Access | 3-4 minutes | High | Phase 1 complete |
| Phase 3: Management & Utilities | 2-3 minutes | Low | Phase 2 complete |
| Testing & Validation | 2-3 minutes | Medium | All phases complete |
π Total Estimated Duration: 9-13 days
- βοΈ Django 4.0+ project with custom User model
- βοΈ PostgreSQL database (required for advanced indexing)
- βοΈ Existing authentication system (django-allauth recommended)
- βοΈ Python 3.9+ environment
- Copy implementation-prompt.md
- Paste into ChatGPT, Claude, or GitHub Copilot Chat
- Follow the AI's step-by-step guidance customized to your project
- Understand the architecture - Read Overview & Architecture
- Plan your approach - Review Requirements and Risks & Success Criteria
- Implement phase by phase - Follow phases 1-3 in order
- Test thoroughly - Use Testing Strategy
- Copy code examples - Reference Code Examples & Configuration
Use Quick Reference as a cheat sheet while implementing.
- πΎ Database Backup - Always backup before migration
- π§ͺ Staging Testing - Test all migrations on staging first
- π Gradual Rollout - Use feature flags for controlled deployment
- β‘ Performance Monitoring - Monitor query performance impact
- π Security Validation - Test cross-tenant isolation thoroughly
Request β Authentication β TenantMiddleware β View Decorator β Tenant-Scoped Query
β
Set request.tenant
Set request.user_role
Key Components:
- Tenant Model: Organization/company data with settings
- UserTenantMembership: Many-to-many relationship with roles
- TenantMiddleware: Automatic tenant context detection
- Decorators:
@require_tenant_access,@require_role - Managers:
.for_tenant()query filtering
Contributions are welcome! Please read our Contributing Guidelines for details on:
- Reporting bugs
- Suggesting enhancements
- Submitting pull requests
- Code style guidelines
Please note that this project is released with a Code of Conduct. By participating in this project you agree to abide by its terms.
This project is licensed under the MIT License - see the LICENSE file for details.
- π Documentation: Check the implementation phase documents
- π Bug Reports: Open an issue with the bug template
- π‘ Feature Requests: Open an issue with the feature request template
- β Questions: Open an issue with the question label
- Built on proven Django multi-tenancy patterns
- Inspired by real-world production implementations
- Community contributions and feedback
If you find this project helpful, please consider giving it a star on GitHub!
Made with β€οΈ for the Django community
