Django和SaaS。如何为每个Django站点使用单独的数据库? [英] Django and SaaS. How to use separate database for each Django site?

查看:216
本文介绍了Django和SaaS。如何为每个Django站点使用单独的数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用Django创建一个SaaS项目。
我决定将 django-saas-kit 用于用户订阅和多-帐户部分。

I am creating a SaaS project with Django. I decided to use django-saas-kit for the user subscriptions and multi-accounts part.

理想情况下,我希望能够为每个用户和一个单独的数据库创建一个新站点。
网站框架是否支持此功能?

Ideally I would like to be able to create a new site for each user and a separate database. Does the sites framework support this? How can it be implemented?

谢谢。

推荐答案

创建一个客户端文件夹,并为每个客户端创建一个子目录。在每个子目录中,创建一个site_settings.py文件,如下所示:

You should create a "clients" folder, and a subdirectory per client. In each subdirectory, create a site_settings.py file as such:

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开关来管理命令。例如:

Then don't forget to use the --settings switch for management commands. For example:

./manage.py syncdb --settings=clients.demo.site_settings

别忘了每个客户都需要自己准备一些额外的东西。例如,如果您将干草堆与whoosh一起使用,则需要添加它,以免客户端之间混淆:

Don't forget that each client will require to have his own extra stuff. For example, if you use haystack with whoosh, you need to add this so that it doesn't get mixed up between clients:

HAYSTACK_WHOOSH_PATH = os.path.join(CLIENT_ROOT, 'whoosh')

或使用django-zstask:

Or with django-zstask:

ZTASKD_URL = 'ipc:///tmp/%s_ztask.sock' % SITE_NAME

或使用JohnnyCache:

Or with JohnnyCache:

JOHNNY_MIDDLEWARE_KEY_PREFIX=SITE_NAME

这篇关于Django和SaaS。如何为每个Django站点使用单独的数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆