https://github.com/epic-codebase/django_drf_multi_tenancy
https://github.com/epic-codebase/django_drf_multi_tenancy
Last synced: 11 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/epic-codebase/django_drf_multi_tenancy
- Owner: Epic-Codebase
- License: apache-2.0
- Created: 2025-02-25T11:40:29.000Z (11 months ago)
- Default Branch: main
- Last Pushed: 2025-02-25T14:55:41.000Z (11 months ago)
- Last Synced: 2025-02-25T15:32:11.652Z (11 months ago)
- Size: 5.86 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Django DRF Multi-Tenancy with Schema Isolation
A scalable **multi-tenant** system using **Django, Django Rest Framework (DRF), and PostgreSQL** with **schema-level isolation**. Each tenant (a group of companies) gets its own **isolated schema**, ensuring **data security** and **performance optimization**.
---
## π Features
β
**Schema-based Multi-Tenancy** β Each tenant has a separate schema in PostgreSQL.
β
**Automatic Schema Switching** β Requests are routed to the correct schema dynamically.
β
**Shared & Tenant-Specific Apps** β Control which apps are shared vs. tenant-specific.
β
**Scalable & Secure** β Supports adding new tenants without modifying existing data.
β
**Django & DRF Integration** β Provides APIs for tenant management.
---
## **Understanding Schema-Based Multi-Tenancy in Django & DRF**
## **How Schema-Based Multi-Tenancy Works**
Schema-based multi-tenancy is an approach where **each tenant (company) has its own isolated schema** in a single PostgreSQL database. This ensures **data security, isolation, and scalability** while allowing shared authentication and cross-tenant management when required.
Unlike row-based multi-tenancy (where all tenants share the same tables), schema-based multi-tenancy provides:
- **Strong Isolation** β Each companyβs data exists in a separate schema.
- **Better Performance** β Queries operate on smaller, tenant-specific tables.
- **Easier Scaling** β Adding a new tenant means creating a new schema without affecting existing tenants.
## **Multi-Tenancy Structure in Global Dealer**
The **Global Dealer** product supports two types of customers:
1. **Single-Company Customers** β One company (one schema) with multiple users.
2. **Conglomerate Customers** β A parent organization with multiple companies (tenants), each having its own schema.
3. **Tenant-Based Users**:
- **Admin Users** β Manage their specific company (tenant).
- **Regular Users** β Belong to a specific company.
### **Schema Design Example**
Hereβs an example of how PostgreSQL schemas will be structured:
```
public (shared schema)
βββ tenants (stores metadata about all tenants)
βββ users (shared authentication system)
β
βββ company_1 (tenant schema)
β βββ projects
β βββ orders
β βββ invoices
β
βββ company_2 (tenant schema)
β βββ projects
β βββ orders
β βββ invoices
β
βββ company_3 (tenant schema)
β βββ projects
β βββ orders
β βββ invoices
```
Each **tenant schema** (`company_1`, `company_2`, etc.) contains the same **isolated tables**. The `public` schema holds **shared tables**, like user authentication.
## **Handling Tenant Routing in Django**
To manage multi-tenancy, Django will dynamically **switch schemas based on the request**. This is done using middleware that detects the tenant from the request domain and activates the corresponding schema.
**Example Middleware Flow:**
1. User requests `company1.globaldealer.com`.
2. Middleware identifies `company1` as the tenant.
3. Django switches to the `company_1` schema.
4. The request is processed using `company_1`'s data.
## π Installation
### 1οΈβ£ Clone the Repository
```sh
git clone https://github.com/Epic-Codebase/django_drf_multi_tenancy.git
cd django_drf_multi_tenancy
```
### 2οΈβ£ Set Up a Virtual Environment
```sh
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
```
### 3οΈβ£ Install Dependencies
```sh
pip install -r requirements.txt
```
### 4οΈβ£ Configure PostgreSQL for Schema Support
Ensure PostgreSQL is installed and create a database:
```sql
CREATE DATABASE multi_tenant_db;
```
Update **`.env`** with database credentials:
```ini
DATABASE_URL=postgres://username:password@localhost:5432/multi_tenant_db
```
### 5οΈβ£ Apply Migrations
```sh
python manage.py migrate_schemas --shared
```
---
## π Usage
### 1οΈβ£ Create a New Tenant
Run the following Django shell command to create a tenant:
```sh
python manage.py shell
```
```python
from tenants.models import Tenant
tenant = Tenant(domain="company1.globaldealer.com", schema_name="company1")
tenant.save()
```
πΉ This will create **a new schema in PostgreSQL** for `company1`.
### 2οΈβ£ Access Tenant-Specific Data
When a request comes from `company1.globaldealer.com`, Django automatically switches to its schema.
---
## π§ Configuration
Modify the **Django settings** to define shared vs. tenant-specific apps:
```python
SHARED_APPS = [
"django_tenants",
"users", # Shared auth system
]
TENANT_APPS = [
"globaldealer_app", # Tenant-specific app
]
INSTALLED_APPS = list(set(SHARED_APPS + TENANT_APPS))
```
---
## π€ Contributing
Contributions are welcome! To contribute:
1. Fork the repo
2. Create a new branch (`feature-branch`)
3. Submit a Pull Request
---
## π License
This project is licensed under the MIT License.