I'm currently building a multi tenancy app with django-tenants.
However I also want to run my REST API in the same project as django-tenants.
This is where the problem arises, if I were to make my API a shared tenants app every tenant subdomain ends up having their own API.
For example, all registered tenants adopt the API routes as part of their subdomain:
tenant1.example.com/api, tenant2.example.com/api, etc.
I don't want my API to be underneath each tenant, I want to just run it on one domain, preferably api.example.com
Does anyone have any advice on how to set this up? How are other people handling this in their projects?
When dealing with multi-tenancy in Django with a shared API, you need to carefully structure your URL routing and configure your Django project to handle the multi-tenancy at the URL level.
Here's a general approach to achieve this:
Separate API URLs from Tenant URLs:
api
app within your Django project.Use a Single API Subdomain:
api.example.com
.api
app'surls.py
.Configure Django-Tenants:
TENANT_URLCONFS
setting to include theapi
app's URL configuration.Here's an example of how your
urls.py
in theapi
app might look:Then, in your main project's
urls.py
, include the API routes and configure Django-Tenants:This way, the API routes are separate from tenant-specific routes, and the API runs under a dedicated subdomain (
api.example.com
).Ensure that you configure your Django-Tenants settings appropriately and make adjustments to suit your specific requirements. Pay attention to the order of inclusion of URLs to avoid conflicts and ensure that the API routes take precedence over tenant-specific routes.