{"id":25980073,"url":"https://github.com/epic-codebase/django_drf_multi_tenancy","last_synced_at":"2026-03-10T05:03:26.713Z","repository":{"id":279435526,"uuid":"938713274","full_name":"Epic-Codebase/django_drf_multi_tenancy","owner":"Epic-Codebase","description":null,"archived":false,"fork":false,"pushed_at":"2025-02-25T15:56:21.000Z","size":32,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-05T07:36:27.173Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Epic-Codebase.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2025-02-25T11:40:29.000Z","updated_at":"2025-02-25T15:56:25.000Z","dependencies_parsed_at":"2025-02-25T15:33:15.033Z","dependency_job_id":"219c26f3-833c-4425-887f-826a636581a5","html_url":"https://github.com/Epic-Codebase/django_drf_multi_tenancy","commit_stats":null,"previous_names":["epic-codebase/django_drf_multi_tenancy"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/Epic-Codebase/django_drf_multi_tenancy","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Epic-Codebase%2Fdjango_drf_multi_tenancy","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Epic-Codebase%2Fdjango_drf_multi_tenancy/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Epic-Codebase%2Fdjango_drf_multi_tenancy/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Epic-Codebase%2Fdjango_drf_multi_tenancy/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Epic-Codebase","download_url":"https://codeload.github.com/Epic-Codebase/django_drf_multi_tenancy/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Epic-Codebase%2Fdjango_drf_multi_tenancy/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":30325601,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-10T01:36:58.598Z","status":"online","status_checked_at":"2026-03-10T02:00:06.579Z","response_time":106,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-03-05T07:33:32.474Z","updated_at":"2026-03-10T05:03:26.674Z","avatar_url":"https://github.com/Epic-Codebase.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Django DRF Multi-Tenancy with Schema Isolation\n\nA 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**.\n\n---\n\n## 🚀 Features\n\n✅ **Schema-based Multi-Tenancy** – Each tenant has a separate schema in PostgreSQL.  \n✅ **Automatic Schema Switching** – Requests are routed to the correct schema dynamically.  \n✅ **Shared \u0026 Tenant-Specific Apps** – Control which apps are shared vs. tenant-specific.  \n✅ **Scalable \u0026 Secure** – Supports adding new tenants without modifying existing data.  \n✅ **Django \u0026 DRF Integration** – Provides APIs for tenant management.  \n\n---\n\n## **Understanding Schema-Based Multi-Tenancy in Django \u0026 DRF**\n\n## **How Schema-Based Multi-Tenancy Works**\nSchema-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.\n\nUnlike row-based multi-tenancy (where all tenants share the same tables), schema-based multi-tenancy provides:\n- **Strong Isolation** – Each company’s data exists in a separate schema.\n- **Better Performance** – Queries operate on smaller, tenant-specific tables.\n- **Easier Scaling** – Adding a new tenant means creating a new schema without affecting existing tenants.\n\n## **Multi-Tenancy Structure in Global Dealer**\nThe **Global Dealer** product supports two types of customers:\n\n1. **Single-Company Customers** – One company (one schema) with multiple users.\n2. **Conglomerate Customers** – A parent organization with multiple companies (tenants), each having its own schema.\n3. **Tenant-Based Users**:\n   - **Admin Users** – Manage their specific company (tenant).\n   - **Regular Users** – Belong to a specific company.\n\n### **Schema Design Example**\nHere’s an example of how PostgreSQL schemas will be structured:\n\n```\npublic (shared schema)\n│── tenants (stores metadata about all tenants)\n│── users (shared authentication system)\n│\n├── company_1 (tenant schema)\n│   ├── projects\n│   ├── orders\n│   ├── invoices\n│\n├── company_2 (tenant schema)\n│   ├── projects\n│   ├── orders\n│   ├── invoices\n│\n├── company_3 (tenant schema)\n│   ├── projects\n│   ├── orders\n│   ├── invoices\n```\n\nEach **tenant schema** (`company_1`, `company_2`, etc.) contains the same **isolated tables**. The `public` schema holds **shared tables**, like user authentication.\n\n## **Handling Tenant Routing in Django**\nTo 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.\n\n**Example Middleware Flow:**\n1. User requests `company1.globaldealer.com`.\n2. Middleware identifies `company1` as the tenant.\n3. Django switches to the `company_1` schema.\n4. The request is processed using `company_1`'s data.\n\n\n\n## 🛠 Installation\n\n### 1️⃣ Clone the Repository\n```sh\ngit clone https://github.com/Epic-Codebase/django_drf_multi_tenancy.git\ncd django_drf_multi_tenancy\n```\n\n### 2️⃣ Set Up a Virtual Environment\n```sh\npython -m venv venv\nsource venv/bin/activate  # On Windows: venv\\Scripts\\activate\n```\n\n### 3️⃣ Install Dependencies\n```sh\npip install -r requirements.txt\n```\n\n### 4️⃣ Configure PostgreSQL for Schema Support\nEnsure PostgreSQL is installed and create a database:\n```sql\nCREATE DATABASE multi_tenant_db;\n```\nUpdate **`.env`** with database credentials:\n```ini\nDATABASE_URL=postgres://username:password@localhost:5432/multi_tenant_db\n```\n\n### 5️⃣ Apply Migrations\n```sh\npython manage.py migrate_schemas --shared\n```\n\n---\n\n## 🚀 Usage\n\n### 1️⃣ Create a New Tenant\nRun the following Django shell command to create a tenant:\n```sh\npython manage.py shell\n```\n```python\nfrom tenants.models import Tenant\ntenant = Tenant(domain=\"company1.globaldealer.com\", schema_name=\"company1\")\ntenant.save()\n```\n🔹 This will create **a new schema in PostgreSQL** for `company1`.\n\n### 2️⃣ Access Tenant-Specific Data\nWhen a request comes from `company1.globaldealer.com`, Django automatically switches to its schema.\n\n---\n\n## 🔧 Configuration\n\nModify the **Django settings** to define shared vs. tenant-specific apps:\n\n```python\nSHARED_APPS = [\n    \"django_tenants\",\n    \"users\",  # Shared auth system\n]\n\nTENANT_APPS = [\n    \"globaldealer_app\",  # Tenant-specific app\n]\n\nINSTALLED_APPS = list(set(SHARED_APPS + TENANT_APPS))\n```\n\n---\n\n## 🤝 Contributing\n\nContributions are welcome! To contribute:\n1. Fork the repo  \n2. Create a new branch (`feature-branch`)  \n3. Submit a Pull Request  \n\n---\n\n## 📜 License\n\nThis project is licensed under the MIT License.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fepic-codebase%2Fdjango_drf_multi_tenancy","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fepic-codebase%2Fdjango_drf_multi_tenancy","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fepic-codebase%2Fdjango_drf_multi_tenancy/lists"}