Search code examples
pythondjangoweb-applicationssubdomain

Django Isolated Subdomains


Let's say we have a model called Event with a slug name. I'm looking to configure my Django app to basically isolate all my other models into separate apps depending on the event. For example:

"http://annualmeetup.domain.com" # in the form of "http://{}.domain.com".format(e.name)

How would I create completed isolated apps such that my models for users, meetings, and others only work in the context of the given subdomain? I was thinking about writing multiple apps for each event and copying the same models via a command script, but I still don't know how to point an app to a subdomain.


Solution

  • One option is to use django-tenant-schemas which is designed for this purpose:

    Django provides currently no simple way to support multiple tenants using the same project instance, even when only the data is different. Because we don't want you running many copies of your project, you'll be able to have:

    • Multiple customers running on the same instance
    • Shared and Tenant-Specific data
    • Tenant View-Routing (i.e., subdomain mapping)

    This will result in you having separate database schema for each tenant (event, in your case), each with its own isolated models. Without too much effort you can create tenants (events) on the fly - it sounds like your use case would require that.

    Note: tenant-schemas only work with Postgres.