Django 1.2中的多数据库配置 [英] Multiple Database Config in Django 1.2
问题描述
这是一个容易的问题。
我在Django 1.2中了解新的多数据库功能的文档有一些麻烦。主要地,我似乎没有找到一个你如何在你的一个模型中实际使用第二个数据库的例子。
I'm having some trouble understanding the documentation for the new multiple database feature in Django 1.2. Primarily, I cant seem to find an example of how you actually USE the second database in one of your models.
当我在models.py中定义一个新类时我指定要连接到哪个数据库?
When I define a new class in my models.py how do I specify which database I intend on connecting to?
我的settings.py包含类似于 -
My settings.py contains something similar to -
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'modules',
'USER': 'xxx',
'PASSWORD': 'xxx',
},
'asterisk': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'users',
'USER': 'xxxx',
'PASSWORD': 'xxxx',
}
}
编辑:我正在阅读路由器上的文档,如虚拟机。如果其他人正在努力,只要确保你在放弃之前读过它2到3次,那么你就可以放弃了。
I was reading the documentation on routers like a dummy. If anyone else is struggling with this just make sure you read it 2 or 3 times before giving up!
推荐答案
有点复杂。
有很多方法可以实现它。基本上,您需要一些指示哪些模型与哪个数据库相关联的方法。
There are a number of ways you could implement it. Basically, you need some way of indicating which models are associated with which database.
这是我使用的代码;希望它有帮助。
Here's the code that I use; hope it helps.
from django.db import connections
class DBRouter(object):
"""A router to control all database operations on models in
the contrib.auth application"""
def db_for_read(self, model, **hints):
m = model.__module__.split('.')
try:
d = m[-1]
if d in connections:
return d
except IndexError:
pass
return None
def db_for_write(self, model, **hints):
m = model.__module__.split('.')
try:
d = m[-1]
if d in connections:
return d
except IndexError:
pass
return None
def allow_syncdb(self, db, model):
"Make sure syncdb doesn't run on anything but default"
if model._meta.app_label == 'myapp':
return False
elif db == 'default':
return True
return None
这样做的方式是创建一个包含我的模型的数据库名称的文件。在您的情况下,您将创建一个单独的模型
- 名为 asterisk.py
的文件位于同一文件夹中作为您的应用程序的模型。
The way this works is I create a file with the name of the database to use that holds my models. In your case, you'd create a separate models
-style file called asterisk.py
that was in the same folder as the models for your app.
在您的 models.py
文件中,您可以添加
In your models.py
file, you'd add
from asterisk import *
然后,当您实际请求该模型的记录时,它的工作原理如下:
Then when you actually request a record from that model, it works something like this:
-
records = MyModel.object.all()
- 模块为
MyModel
是myapp。星号
- 有一个名为星号的连接,所以使用
而不是默认
records = MyModel.object.all()
- module for
MyModel
ismyapp.asterisk
- there's a connection called "asterisk" so use it instead of "default"
第二个选项
如果要对数据库选择进行每个模型控制,这样就可以运行:
Second Option
If you want to have per-model control of database choice, something like this would work:
from django.db import connections
class DBRouter(object):
"""A router to control all database operations on models in
the contrib.auth application"""
def db_for_read(self, model, **hints):
if hasattr(model,'connection_name'):
return model.connection_name
return None
def db_for_write(self, model, **hints):
if hasattr(model,'connection_name'):
return model.connection_name
return None
def allow_syncdb(self, db, model):
if hasattr(model,'connection_name'):
return model.connection_name
return None
然后对于每个模型:
class MyModel(models.Model):
connection_name="asterisk"
#etc...
请注意,我还没有测试过第二个选项。
Note that I have not tested this second option.
这篇关于Django 1.2中的多数据库配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!