簡體   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