Django 隔离子域
Django Isolated Subdomains
假设我们有一个名为 Event
的模型,其中有一个 slug name
。我希望配置我的 Django 应用程序,以便根据事件基本上将我所有其他模型隔离到单独的应用程序中。例如:
"http://annualmeetup.domain.com" # in the form of "http://{}.domain.com".format(e.name)
我将如何创建完整的独立应用程序,以便我的用户、会议和其他模型仅在给定子域的上下文中工作?我正在考虑为每个事件编写多个应用程序并通过命令脚本复制相同的模型,但我仍然不知道如何将应用程序指向子域。
一种选择是使用专为此目的设计的 django-tenant-schemas:
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)
这将导致您为每个租户(事件,在您的情况下)拥有单独的数据库模式,每个租户都有自己的独立模型。无需太多努力,您就可以即时创建租户(事件)——听起来您的用例需要这样做。
注意:tenant-schemas
仅适用于 Postgres。
您不必为每个活动编写单独的应用程序。如果每个事件都共享相同的架构,您可以将架构设为 Multitenant
这里是所有可用的软件包。 Link here
我个人推荐 django-hosts
但它没有提供良好的数据隔离功能。如果您已经有一个现有项目并且只想引入多租户,这很好。但是,如果您刚刚开始该项目,django-tenancy
是一个不错的选择。
假设我们有一个名为 Event
的模型,其中有一个 slug name
。我希望配置我的 Django 应用程序,以便根据事件基本上将我所有其他模型隔离到单独的应用程序中。例如:
"http://annualmeetup.domain.com" # in the form of "http://{}.domain.com".format(e.name)
我将如何创建完整的独立应用程序,以便我的用户、会议和其他模型仅在给定子域的上下文中工作?我正在考虑为每个事件编写多个应用程序并通过命令脚本复制相同的模型,但我仍然不知道如何将应用程序指向子域。
一种选择是使用专为此目的设计的 django-tenant-schemas:
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)
这将导致您为每个租户(事件,在您的情况下)拥有单独的数据库模式,每个租户都有自己的独立模型。无需太多努力,您就可以即时创建租户(事件)——听起来您的用例需要这样做。
注意:tenant-schemas
仅适用于 Postgres。
您不必为每个活动编写单独的应用程序。如果每个事件都共享相同的架构,您可以将架构设为 Multitenant
这里是所有可用的软件包。 Link here
我个人推荐 django-hosts
但它没有提供良好的数据隔离功能。如果您已经有一个现有项目并且只想引入多租户,这很好。但是,如果您刚刚开始该项目,django-tenancy
是一个不错的选择。