A plug-and-play multi-tenancy package for Django SaaS applications.
Project description
django-multitenant-saas
A plug-and-play multi-tenancy package for Django SaaS apps using shared database and shared schema model.
🚀 Features
- Tenant model with domain-based identification
- Middleware to auto-detect tenant from request host
- Thread-local storage of current tenant
- Abstract base model to scope all tenant-specific data
- Automatic query filtering based on active tenant
📦 Installation
pip install django-multitenant-saas
⚙️ Setup
1. Add to Installed Apps
In your settings.py:
INSTALLED_APPS = [
...
'multitenant',
]
2. Add Middleware
MIDDLEWARE = [
...
'multitenant.middleware.TenantMiddleware',
]
Make sure it's before views or anything that uses request.tenant.
3. Migrate
Run initial migrations:
python manage.py makemigrations multitenant
python manage.py migrate
🧱 Usage
Step 1: Create a Tenant
from multitenant.models import Tenant
Tenant.objects.create(name="Tenant A", domain="tenant-a.localhost")
Step 2: Create Your Models with Tenant Awareness
from multitenant.models import TenantAwareModel
class Invoice(TenantAwareModel):
amount = models.DecimalField(max_digits=10, decimal_places=2)
description = models.TextField()
This will automatically:
- Add a
tenantForeignKey - Auto-set
tenantfrom the request - Ensure query filtering by current tenant
Step 3: Query Your Data (Filtered Automatically)
Invoice.objects.all() # Automatically scoped to current request's tenant
🔍 Behind the Scenes
Middleware
Detects the domain from the request and fetches the tenant:
host = request.get_host().split(':')[0]
Tenant.objects.get(domain=host)
Thread-Local Storage
The tenant is stored per-request using Python’s threading.local.
Model Inheritance
Your custom models inherit TenantAwareModel, which auto-fills and filters by tenant.
🛡 Security Note
- Always scope views and queries using the
TenantAwareModelorTenantManager. - Do not allow direct access to
tenantfield in serializers unless validated.
📚 Example Project Structure
myproject/
├── myapp/
│ └── models.py # Inherit from TenantAwareModel
├── multitenant/ # Installed from package
│ └── ...
├── settings.py # Add app and middleware
🧪 Testing (Optional)
You can add custom middleware tests or unit tests to simulate tenant context.
📬 Support
Have questions or want to contribute? Reach out or fork the repo. Built by Pranav Dixit
MIT License
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django_multitenant_saas-0.1.0.tar.gz.
File metadata
- Download URL: django_multitenant_saas-0.1.0.tar.gz
- Upload date:
- Size: 4.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
92b22ef6bda2bb0ea163e96c87780a0516596945516c13ba9421e51affcd02eb
|
|
| MD5 |
a293b54f13559e66fc3cb0578198c1fe
|
|
| BLAKE2b-256 |
c563efaea943852828d3a55864c1860400755a95c4e0e6a936d0782ca2e722a8
|
File details
Details for the file django_multitenant_saas-0.1.0-py3-none-any.whl.
File metadata
- Download URL: django_multitenant_saas-0.1.0-py3-none-any.whl
- Upload date:
- Size: 4.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
17a8ec199a853e0786ede77998d3d43688c6e2d7fd23137bd3bf85bfcd75cb74
|
|
| MD5 |
f6f595f403dd3b54bece60ce547461c7
|
|
| BLAKE2b-256 |
e15551dea0df67f6162e794fdf716cedf5aa37f8a2e7caa34dd5cedf2a147350
|