简体   繁体   English

django中的多个数据库支持

[英]Multiple database support in django

From some forum I came to know that Multiple database support is added in Django at lower level, but the higher level apis are not added yet. 从一些论坛我开始知道Django在较低级别添加了多个数据库支持,但是尚未添加更高级别的api。

Can anyone please tell me how one can achieve multiple database connections in Django. 任何人都可以告诉我如何在Django中实现多个数据库连接。

Does anyone have any idea by when Django will fully/officially support Multiple database connections. 有没有人知道Django何时完全/正式支持多个数据库连接。

If you simply need multiple connections, you can do something like this: 如果您只需要多个连接,则可以执行以下操作:

from django.db import load_backend
myBackend = load_backend('postgresql_psycopg2') # or 'mysql', 'sqlite3', 'oracle'
myConnection = myBackend.DatabaseWrapper({
    'DATABASE_HOST': '192.168.1.1',
    'DATABASE_NAME': 'my_database',
    'DATABASE_OPTIONS': {},
    'DATABASE_PASSWORD': "",
    'DATABASE_PORT': "",
    'DATABASE_USER': "my_user",
    'TIME_ZONE': "America/New_York",})
# Now we can do all the standard raw sql stuff with myConnection.
myCursor = myConnection.cursor()
myCursor.execute("SELECT COUNT(1) FROM my_table;")
myCursor.fetchone()

我在其上看到的最新讨论是在Proposal中:用于多数据库支持的用户友好API django-developers线程,其中还有一个使用原始消息中的Managers使用多个数据库的方法的示例。

If you read a few of the many ( many ) threads on this subject in django-dev, you will see that what looks straightforward, isn't. 如果你在django-dev中阅读了关于这个主题的很多( 很多 )线程中的一些,你会看到看起来很简单的东西,不是。 If you pick a single use case, then it looks easy, but as soon as you start to generalize in any way you start to run into trouble. 如果您选择一个用例,那么它看起来很简单,但只要您开始以任何方式进行概括,就会开始遇到麻烦。

To use the above-referenced thread as an example, when you say "multiple databases", which of the following are you talking about? 要使用上面引用的线程作为示例,当您说“多个数据库”时,您在谈论以下哪个?

  • All DB on the same machine under the same engine. 同一台机器上的所有DB都在同一台引擎下。
  • All DB on same machine, different engines (Eg MySQL + PostgreSQL) 所有数据库在同一台机器上,不同的引擎(例如MySQL + PostgreSQL)
  • One Master DB with N read-only slaves on different machines. 一个主DB,在不同的机器上有N个只读从站。
  • Sharding of tables across multiple DB servers. 跨多个数据库服务器对表进行分片。

Will you need: 你需要:

  • Foreign keys across DBs 跨DB的外键
  • JOINs across machines and/or engines 跨机器和/或引擎连接
  • etc. etc. 等等

One of the problems with a slick ORM like Django's is that it hides all of those messy details under a nice paint job. 像Django这样光滑的ORM的一个问题是它在一个漂亮的油漆工作下隐藏了所有这些杂乱的细节。 To continue to do that, but to then add in any of the above, is Not Easy (tm). 要继续这样做,但要添加上述任何一项,都不容易(tm)。

Eric Florenzano wrote a very good blog post that allows you some multiple database support at: Easy MultipleDatabase Support for Django . Eric Florenzano撰写了一篇非常好的博客文章,允许您在以下方面获得多个数据库支持: Easy MultipleDatabase支持Django

It starts by creating a new custom manager that allows you to specify the database settings. 首先,创建一个允许您指定数据库设置的新自定义管理器。

Multiple database to choose from 多个数据库可供选择

We always need one named default, the names of the rest are up to you. 我们总是需要一个命名默认值,其余的名称由您决定。

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'mupltiple_datab_app1',                     
        'USER': 'root',                     
        'PASSWORD': 'admin',                  
        'HOST': "",                      
        'PORT': "",                     
    },
    'user1':{
        'ENGINE': 'django.db.backends.mysql', 
        'NAME': 'mupltiple_datab_app2',                      
        'USER': 'root',                     
        'PASSWORD': 'admin',                  
        'HOST': "",                        
        'PORT': "",  

    },
    'user2':{
        'ENGINE': 'django.db.backends.mysql', 
        'NAME': 'mupltiple_datab_app3',                      
        'USER': 'root',                     
        'PASSWORD': 'admin',                  
        'HOST':"" ,                     
        'PORT': "" ,  

    }
}

for sync to one particular database 用于同步到一个特定数据库

manage.py syncdb --database=user1

There is a "using" directive for queries,saves, and deletes 查询,保存和删除都有一个“using”指令

https://docs.djangoproject.com/en/dev/topics/db/multi-db/#manually-selecting-a-database https://docs.djangoproject.com/en/dev/topics/db/multi-db/#manually-selecting-a-database

Eric Florenzano's approach works well if all your databases use the same engine. 如果所有数据库都使用相同的引擎,Eric Florenzano的方法很有效。 If you have different engines (Postgres and MSSQL in my case) you will run into many issues deep in the ORM code (such as models/sql/where.py using the default connection's SQL syntax). 如果您有不同的引擎(在我的情况下是Postgres和MSSQL),您将在ORM代码中遇到很多问题(例如使用默认连接的SQL语法的models / sql / where.py)。

If you need this to work, you should wait for Alex Gaynor's MultiDB project which is planned for Django 1.2 如果你需要这个工作,你应该等待Alex Gaynor计划用于Django 1.2的MultiDB项目

From Django 1.2, it will support multiple databases. 从Django 1.2开始,它将支持多个数据库。 See: http://docs.djangoproject.com/en/dev/topics/db/multi-db/ Version 1.2 is now in beta 请参阅: http//docs.djangoproject.com/en/dev/topics/db/multi-db/版本1.2现在处于测试阶段

I think you will have to resort to "raw sql" .. kinda thing .. 我想你将不得不求助于“原始sql”..有点事......
look here: http://docs.djangoproject.com/en/dev/topics/db/sql/ 看这里: http//docs.djangoproject.com/en/dev/topics/db/sql/

you need a "connection" to your other database, if you look at django/db/__init__.py around line 39 (in my version ..) 你需要与你的其他数据库“连接”,如果你看看第39行的django/db/__init__.py (在我的版本中..)

connection = backend.DatabaseWrapper(**settings.DATABASE_OPTIONS)

try to take it from there .. 试着从那里拿走它..
PS I haven't really tried this or anything .. just trying to point in the general direction of what I think might solve your problem. PS我还没有真正尝试过这个或任何事情......只是想指出我认为可以解决你的问题的大方向。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM