Django 上具有子域的多个 PostgreSQL 架构和用户
我看到过有关多站点和多主机 Django 的各种问题,包括子域和每个子域的特定架构。我还没有看到这个问题的解决方案(或提示,以便我可以编写一个解决方案)。
- 我在一个网站上使用 Django + PostgreSQL,比方说 myapp.com
- 主网站 myapp.com 用于公司
- 注册 注册公司拥有自己的子域 company.myapp.com,并从那里登录并工作。
我的想法是在 PostgreSQL 中创建 2 个初始模式。
- 公司和用户的架构“auth” 架构
- “empty_company_template”,包含公司的基本表,全部为空,但连接到正确的序列等。
当新公司注册时,我希望发生这种情况:
- 为公司创建一个新架构,派生自empty_company_template
- 为公司创建一个新的数据库用户,名为company(公司名称)
- 将此新用户的搜索路径设置为company,auth(无法访问empty_company_template,无法访问其他用户模式)
对我来说这似乎更好与现有的解决方案相比,整个应用程序似乎都依赖于一个数据库用户(可以访问所有方案)。然而,我很难让它发挥作用。这确实是一个可行的方法吗?有人能指出我正确的方向吗?它是 Django,所以也许它已经完成了,只是我还没有找到它?
I have seen various questions on multi-site and multi-host Django, including subdomains and specific schemas per subdomain. What I have not seen is a solution (or tips so I can code one) to this problem.
- I am using Django + PostgreSQL on a site, let's say myapp.com
- The main site myapp.com is used for registration of companies
- A registered company gets its own subdomain, company.myapp.com, and logs in and works from there.
My idea of doing this is by making 2 initial schemas in PostgreSQL.
- Schema "auth" for companies and users
- Schema "empty_company_template" with the basic tables for a company, all empty but hooked up to the right sequences etc.
When a new company registers, I want this to happen:
- Create a new schema for the company, derives from empty_company_template
- Create a new DB user for the company, named company (the company name)
- Set the search path for this new user to company, auth (no access to empty_company_template, no access to other users schema's)
To me this seems better than the existing solutions that all seem to depend on one single database user for the entire application (with access to all schemes). However, I struggle to get this to work. Is this indeed a viable approach? Can anyone point me in the right direction? It's Django, so perhaps it's been done and I just haven't found it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我有一个可行的解决方案,可以完成除单独用户之外的所有操作。
它是一小段中间件(只是 process_request),用于确定子域,并在数据库上执行 SET search_path 查询。目前对我来说已经足够了。
对代码感兴趣的朋友可以联系我。当它最终确定时,我会在某个地方发布它。
编辑 2010 年 12 月 22 日:
我在我的博客上发布了代码
http://blog.dyve.net/django-subdomains-and-postgresql-schemas
I have a working solution that does everything except separate users.
It's a small piece of middleware (just process_request) that determines the subdomain, and executes a SET search_path query on the database. It's good enough for me for now.
Anyone interested in the code, just contact me. I'll publish it somewhere when it's final.
EDIT Dec 22, 2010:
I published the code on my blog at
http://blog.dyve.net/django-subdomains-and-postgresql-schemas
如果您想要拥有单独的数据库用户,您可能需要单独的 Django 实例,否则将不会获得安全性。该模型将需要更加复杂的流程管理。我认为您不会为此类应用程序找到现成的解决方案,因此您很可能必须推出自己的解决方案。否则,如果您不愿意投入太多时间,请坚持使用一个用户来完成整个应用程序的方法。
If you want to have separate DB users, you'll probably want separate Django instances, otherwise there will be no security gain. This model will require much more complicated process management. I don't think you'll find a ready-made solution for such an application, so you will most probably have to roll your own. Otherwise, if you're unwilling to invest much time, stick with the one user for the entire app approach.