In django, most multi-tenant implementations (modules) are mapping hosts onto views.
在django中,大多数多租户实现(模块)都是将主机映射到视图。
e.g. mapping host/URL -> django view using postgress schema's:
例如,映射主机/URL -使用后继模式的> django视图:
customer1.myapp.com/view1/arg1 -> myapp.view1(arg1) using schema 'customer1'
customer2.myapp.com/view1/arg1 -> myapp.view1(arg1) using schema 'customer2'
customer3.myapp.com/view1/arg1 -> myapp.view1(arg1) using schema 'customer3'
Since my PaaS (Pythonanywhere) is not supporting wildcard domains (*.myapp.com), I am trying to set up a multi-tenant application using URL mapping:
由于我的PaaS (Pythonanywhere)不支持通配符域(*.myapp.com),所以我尝试使用URL映射建立一个多租户应用程序:
e.g. mapping URL -> django view:
例如,映射URL -> django视图:
myapp.com/customer1/view1/arg1 -> myapp.view1(arg1) passing implicit parameter tenant='customer1'
myapp.com/customer2/view1/arg1 -> myapp.view1(arg1) passing implicit parameter tenant='customer2'
myapp.com/customer3/view1/arg1 -> myapp.view1(arg1) passing implicit parameter tenant='customer3'
Here some middleware should take care of passing the tenant parameter to the view and filtering query results for objects applicable to the selected tenant. e.g. https://django-tenant-schemas.readthedocs.io/en/latest/
在这里,一些中间件应该负责将承租者参数传递给视图,并过滤适用于所选承租者的对象的查询结果。例如,https://django-tenant-schemas.readthedocs.io/en/latest/
But question here is: How to do this -which package can handle this- for URL mapping instead of host mapping?
但是这里的问题是:对于URL映射而不是主机映射,如何做这个包可以处理这个?
Note: django-multitenants mentions "Supports url patterns as well as sub-domains" but not clear how to do this... https://pypi.python.org/pypi/django-multitenants
注意:django-multitenant提到“支持url模式和子域”,但不清楚如何做到这一点……https://pypi.python.org/pypi/django-multitenants
2 个解决方案
#1
1
Solved with great help from Pythonanywhere support in following way:
在Pythonanywhere的大力帮助下解决了以下问题:
- Create a new webapp for each tenant (yes, this is costing 2$ / month)
- 为每个租户创建一个新的webapp(是的,这将花费2美元/月)
- Map the new domain to the new webapp (check pythonanywhere web tab "DNS setup")
- 将新域映射到新的webapp(查看pythonanywhere web选项卡“DNS设置”)
- Have each webapp use the same code and the same postgres database (copy static & media patsh & wsgi script)
- 让每个webapp使用相同的代码和相同的postgres数据库(复制静态& media patsh & wsgi脚本)
- Create the new schema in the postgres DB
- 在postgres DB中创建新的模式
- The rest is easy: follow the documentation of django-tenant-schemas
- 剩下的很简单:遵循django-tenant-schemas的文档
Roughly:
约:
- Sync the new schema: ./manage.py migrate_schemas --schema=my-new-tenant
- 同步新的模式:./管理。py migrate_schemas = my-new-tenant——模式
- Add the new tenant to the public tenant "customers_client" table
- 将新租户添加到公共租户“customers_client”表。
- And if your user model is NOT in the public table: create new admin user for this tenant: ./manage.py createsuperuser --schema=my-new-tenant
- 如果您的用户模型不在公共表中:为这个承租者:./manage创建新的管理用户。py createsuperuser = my-new-tenant——模式
#2
0
From a bit of poking around in django-multitenants
, it looks like it's meant to support URL-based multitenancy using a setting called TENANT_BASE_PATH
通过在django多租户中进行一些探索,看起来它应该使用一个名为TENANT_BASE_PATH的设置来支持基于url的多租户
However... if you search the codebase of the project on GitHub, the only references to that setting appear in the documentation, not in the code itself. So it may be that django-multitenants
isn't a finished project, but rather a work-in-progress that may have been abandoned (last commit was just 14 days after the initial commit).
然而……如果在GitHub上搜索项目的代码基,对该设置的惟一引用将出现在文档中,而不是代码本身中。因此,django多租户并不是一个完成的项目,而是一个可能已经被放弃的正在进行的工作(上一次提交是在最初提交之后的14天)。
#1
1
Solved with great help from Pythonanywhere support in following way:
在Pythonanywhere的大力帮助下解决了以下问题:
- Create a new webapp for each tenant (yes, this is costing 2$ / month)
- 为每个租户创建一个新的webapp(是的,这将花费2美元/月)
- Map the new domain to the new webapp (check pythonanywhere web tab "DNS setup")
- 将新域映射到新的webapp(查看pythonanywhere web选项卡“DNS设置”)
- Have each webapp use the same code and the same postgres database (copy static & media patsh & wsgi script)
- 让每个webapp使用相同的代码和相同的postgres数据库(复制静态& media patsh & wsgi脚本)
- Create the new schema in the postgres DB
- 在postgres DB中创建新的模式
- The rest is easy: follow the documentation of django-tenant-schemas
- 剩下的很简单:遵循django-tenant-schemas的文档
Roughly:
约:
- Sync the new schema: ./manage.py migrate_schemas --schema=my-new-tenant
- 同步新的模式:./管理。py migrate_schemas = my-new-tenant——模式
- Add the new tenant to the public tenant "customers_client" table
- 将新租户添加到公共租户“customers_client”表。
- And if your user model is NOT in the public table: create new admin user for this tenant: ./manage.py createsuperuser --schema=my-new-tenant
- 如果您的用户模型不在公共表中:为这个承租者:./manage创建新的管理用户。py createsuperuser = my-new-tenant——模式
#2
0
From a bit of poking around in django-multitenants
, it looks like it's meant to support URL-based multitenancy using a setting called TENANT_BASE_PATH
通过在django多租户中进行一些探索,看起来它应该使用一个名为TENANT_BASE_PATH的设置来支持基于url的多租户
However... if you search the codebase of the project on GitHub, the only references to that setting appear in the documentation, not in the code itself. So it may be that django-multitenants
isn't a finished project, but rather a work-in-progress that may have been abandoned (last commit was just 14 days after the initial commit).
然而……如果在GitHub上搜索项目的代码基,对该设置的惟一引用将出现在文档中,而不是代码本身中。因此,django多租户并不是一个完成的项目,而是一个可能已经被放弃的正在进行的工作(上一次提交是在最初提交之后的14天)。