Our current set up has Django hosted on Google's appengine with a MySQL database on Google's Cloud SQL.
我们目前的设置是让Django在谷歌的Cloud SQL上安装了一个MySQL数据库。
The users (clients) are typically small businesses who we give a subdomain to for a multi-tenant database structure (1 database for each client).
用户(客户端)通常是小型企业,我们为多租户数据库结构(每个客户端1个数据库)提供子域。
As for determining which request should hit up which database, there is an existing middleware which strips the request path to get the subdomain and thus returning the correlated database alias defined in settings.py
至于确定哪个请求应该占用哪个数据库,有一个现有的中间件剥离请求路径以获取子域,从而返回settings.py中定义的相关数据库别名
from django.conf import settings
import threading
currentCompanyConnection = threading.local()
class DatabaseMiddleware(object):
def process_request(self, request):
url = request.build_absolute_uri()
if url.find("http://") < 0:
company = url.split("https://")[1].split(".")[0]
else:
company = url.split("http://")[1].split(".")[0]
global currentCompanyConnection
if company in settings.DATABASES:
currentCompanyConnection.company = company
else:
currentCompanyConnection.company = 'default'
request.currentCompany = str(currentCompanyConnection.company)
return None
class DBRouter(object):
def read_and_write(self, model, **hints):
return currentCompanyConnection.company
db_for_read = read_and_write
db_for_write = read_and_write
However, to allow our web application the functionality of a freemium self-service, a new database must be generated on the fly and imported into Django's settings.py dynamically for each user who sign up.
但是,为了使我们的Web应用程序具有免费增值自助服务的功能,必须动态生成新数据库,并为每个注册用户动态导入Django的settings.py.
The last part is something I can't seem to figure out, since each time I change the settings.py, I must deploy it to appengine again. Aside from that, I'm not sure how to create a new database with pre-defined tables in Google's Cloud SQL from our web application.
最后一部分是我似乎无法弄清楚的,因为每次我更改settings.py时,我都必须将它再次部署到appengine。除此之外,我不确定如何使用我们的Web应用程序中的Google Cloud SQL中的预定义表创建新数据库。
Thanks for your help! I love resolving challenges from work, but this is something I simply haven't done before =O
谢谢你的帮助!我喜欢解决工作中的挑战,但这是我以前没有做过的事情= O.
1 个解决方案
#1
1
You can't modify your source files once deployed. You can modify stuff in the blobstore or datastore.
部署后,您无法修改源文件。您可以修改blobstore或数据存储区中的内容。
I'd recommend storing the settings as structured data in the datastore, then have your settings.py read the data from the datastore and store them as python objects in settings.py that are accessible from other code. This should allow you to configure django to connect to multiple databases.
我建议将设置作为结构化数据存储在数据存储区中,然后让您的settings.py从数据存储区读取数据,并将它们存储为settings.py中可从其他代码访问的python对象。这应该允许您配置django以连接到多个数据库。
I'm not too familiar with CloudSQL, but I think you may still have a challenge of starting multiple CloudSQL instances and routing the appropriate traffic to the appropriate instances.
我对CloudSQL并不太熟悉,但我认为您可能仍然面临着启动多个CloudSQL实例并将适当的流量路由到适当实例的挑战。
#1
1
You can't modify your source files once deployed. You can modify stuff in the blobstore or datastore.
部署后,您无法修改源文件。您可以修改blobstore或数据存储区中的内容。
I'd recommend storing the settings as structured data in the datastore, then have your settings.py read the data from the datastore and store them as python objects in settings.py that are accessible from other code. This should allow you to configure django to connect to multiple databases.
我建议将设置作为结构化数据存储在数据存储区中,然后让您的settings.py从数据存储区读取数据,并将它们存储为settings.py中可从其他代码访问的python对象。这应该允许您配置django以连接到多个数据库。
I'm not too familiar with CloudSQL, but I think you may still have a challenge of starting multiple CloudSQL instances and routing the appropriate traffic to the appropriate instances.
我对CloudSQL并不太熟悉,但我认为您可能仍然面临着启动多个CloudSQL实例并将适当的流量路由到适当实例的挑战。