2👍
The problem you have is not new, but a common one for anyone creating any multi-tenant software; or developing a software-as-a-service application.
For a more detailed analysis on the data challenges associated with such an architecture, I highly recommend reading this excellent article from 2006 from MSDN.
The django-tenant-schemas
application provides view routing so that domains are correctly routed to their individual databases:
Tenants are identified via their host name (i.e tenant.domain.com).
This information is stored on a table on the public schema. Whenever a
request is made, the host name is used to match a tenant in the
database. If there’s a match, the search path is updated to use this
tenant’s schema. So from now on all queries will take place at the
tenant’s schema. For example, suppose you have a tenant customer at
http://customer.example.com. Any request incoming at
customer.example.com will automatically use customer‘s schema and make
the tenant available at the request. If no tenant is found, a 404
error is raised. This also means you should have a tenant for your
main domain, typically using the public schema.