Python Django 中的多个数据库和多个模型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18547468/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
multiple databases and multiple models in django
提问by kuafu
I have two databases and two models:the Admin and the user.
我有两个数据库和两个模型:管理员和用户。
I want to sync my models to the two databases; admin model to database A and user model to database B;
我想将我的模型同步到两个数据库;数据库 A 的管理模型和数据库 B 的用户模型;
If I am setting the model path to INSTALLED_APPS
and syncdb
, the two models will sync to the default database.
如果我将模型路径设置为INSTALLED_APPS
和syncdb
,两个模型将同步到默认数据库。
if I set the database in the syncdb
command such as sync --database="B"
, and the two models will sync to database B.
如果我在syncdb
诸如 之类的命令中设置数据库sync --database="B"
,则两个模型将同步到数据库 B。
So my problem is, how do I sync the two models to two databases?
所以我的问题是,如何将两个模型同步到两个数据库?
回答by alecxe
In order to define specific databases used for specific models, you need to define a database router:
为了定义用于特定模型的特定数据库,您需要定义一个数据库路由器:
The easiest way to use multiple databases is to set up a database routing scheme. The default routing scheme ensures that objects remain ‘sticky' to their original database (i.e., an object retrieved from the foo database will be saved on the same database). The default routing scheme ensures that if a database isn't specified, all queries fall back to the default database.
使用多个数据库的最简单方法是设置数据库路由方案。默认路由方案确保对象保持“粘性”到其原始数据库(即,从 foo 数据库检索的对象将保存在同一数据库中)。默认路由方案确保如果未指定数据库,则所有查询都回退到默认数据库。
See this snippet as an example: http://djangosnippets.org/snippets/2687/
以这个片段为例:http: //djangosnippets.org/snippets/2687/
Also see:
另见:
回答by Adam Lewis
I fully agree with @alecxe on using the database router. I am currently using a single admin interface to manage multiple databases. Note that authentication for all databases are stored in the default database, so when you do the syncdb
(with no arguments).
我完全同意@alecxe 关于使用数据库路由器的看法。我目前使用单个管理界面来管理多个数据库。请注意,所有数据库的身份验证都存储在默认数据库中,因此当您执行syncdb
(不带参数)时。
Generic Database Router
通用数据库路由器
I found thisimplementation to be extremely flexible and useful.
我发现这个实现非常灵活和有用。
Settings.py
设置.py
# Define the database manager to setup the various projects
DATABASE_ROUTERS = ['manager.router.DatabaseAppsRouter']
DATABASE_APPS_MAPPING = {'mux_data': 't29_db',
'T50_VATC':'t50_db'}
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'fail_over',
'USER': 'SomeUser',
'PASSWORD': 'SomePassword',
'HOST': '127.0.0.1',
'PORT': '',
},
't29_db': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'mux_stage',
'USER': 'SomeUser',
'PASSWORD': 'SomePassword',
'HOST': '127.0.0.1',
'PORT': '',
},
't50_db': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 't50_vatc',
'USER': 'SomeUser',
'PASSWORD': 'SomePassword',
'HOST': '127.0.0.1',
'PORT': '',
},
}
Sample Models
示例模型
# Create your models here.
class Card_Test(models.Model):
name = models.TextField(max_length=100)
description = models.TextField(max_length=200)
units = models.TextField(max_length=500)
result_tags = models.TextField(max_length=500)
class Meta:
app_label = 'mux_data'
def __unicode__(self):
return self.name
class Status_Type(models.Model):
status = models.CharField(max_length=25)
class Meta:
app_label = 'mux_data'
def __unicode__(self):
return self.status