[英]Django and SaaS. How to use separate database for each Django site?
我正在使用Django創建一個SaaS項目。 我決定將django-saas-kit用於用戶訂閱和多帳戶部分。
理想情況下,我希望能夠為每個用戶和一個單獨的數據庫創建一個新站點。 網站框架是否支持這一點? 如何實施?
謝謝。
您應該創建一個“客戶端”文件夾,並為每個客戶端創建一個子目錄。 在每個子目錄中,如下創建一個site_settings.py文件:
import os.path
# import global settings
from settings import *
# this is barely just the name of the client dir, you might want to use that
SITE_NAME = __file__.split('/')[-2]
# this is the directory of the client website
CLIENT_ROOT = os.path.abspath(os.path.dirname(__file__))
DATABASES = {
'default': {
'ENGINE': 'django.contrib.gis.db.backends.postgis',
'NAME': SITE_NAME,
'USER': SITE_NAME,
'PASSWORD': 'some random password',
}
}
# you might want this too so that each client have his own MEDIA_ROOT
MEDIA_ROOT = os.path.join(CLIENT_ROOT, 'upload')
然后,不要忘記使用--settings開關來管理命令。 例如:
./manage.py syncdb --settings=clients.demo.site_settings
不要忘記,每個客戶都需要擁有自己的額外東西。 例如,如果您將干草堆與whoosh一起使用,則需要添加此代碼,以免客戶端之間混淆:
HAYSTACK_WHOOSH_PATH = os.path.join(CLIENT_ROOT, 'whoosh')
或使用django-zstask:
ZTASKD_URL = 'ipc:///tmp/%s_ztask.sock' % SITE_NAME
或與JohnnyCache:
JOHNNY_MIDDLEWARE_KEY_PREFIX=SITE_NAME
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.