简体   繁体   English

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

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

If I am using django-tenant-schemas (or django-tenants fork), the way they appear to be set up is that you would access the tenants on a separate subdomain or separate domain.如果我使用 django-tenant-schemas(或 django-tenants fork),它们的设置方式似乎是您将访问单独子域或单独域上的租户。

This means accessing tenant1 using tenant1.mysite.com or tenant1.com.这意味着使用tenant1.mysite.com 或tenant1.com 访问tenant1。

However, I would like to access tenants using mysite.com/tenant1/, mysite.com/tenant2/.但是,我想使用 mysite.com/tenant1/、mysite.com/tenant2/ 访问租户。

This is because I am using DRF, and so I just want their information stored separately.这是因为我使用的是 DRF,所以我只想将他们的信息分开存储。 And I want to access them via the API in the same way.我想以同样的方式通过 API 访问它们。 So, for instance I can make calls to mysite.com/tenant1/api/token/.因此,例如,我可以调用 mysite.com/tenant1/api/token/。

How would I set this up?我该如何设置?

You can use a custom middleware then substitute the default middleware with it.您可以使用自定义中间件,然后用它替换默认中间件。 Advanced usage section of Django Tenant Schema documentation (which, i guess, is also the same in Django Tenants) described how to do that. Django 租户架构文档的高级用法部分(我想,在 Django 租户中也是相同的)描述了如何做到这一点。 Below is an example of how to use custom HTTP Headers.下面是如何使用自定义 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. 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