繁体   English   中英

在同一个域上使用 Django 租户,而不是子域或多个域

[英]Using Django tenants on the same domain, rather than subdomains or multiple domains

如果我使用 django-tenant-schemas(或 django-tenants fork),它们的设置方式似乎是您将访问单独子域或单独域上的租户。

这意味着使用tenant1.mysite.com 或tenant1.com 访问tenant1。

但是,我想使用 mysite.com/tenant1/、mysite.com/tenant2/ 访问租户。

这是因为我使用的是 DRF,所以我只想将他们的信息分开存储。 我想以同样的方式通过 API 访问它们。 因此,例如,我可以调用 mysite.com/tenant1/api/token/。

我该如何设置?

您可以使用自定义中间件,然后用它替换默认中间件。 Django 租户架构文档的高级用法部分(我想,在 Django 租户中也是相同的)描述了如何做到这一点。 下面是如何使用自定义 HTTP 标头的示例。

class XHeaderTenantMiddleware(BaseTenantMiddleware):
"""
Determines tenant by the value of the ``X-DTS-SCHEMA`` HTTP header.
"""
def get_tenant(self, model, hostname, request):
    schema_name = request.META.get('HTTP_X_DTS_SCHEMA', get_public_schema_name())
    return model.objects.get(schema_name=schema_name)

In your case, instead of using HTTP Header you can get your tenants by extracting them from the URL path (maybe using string.split("delimiter")) then you set that as the value of the schema_name in the code above.

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM