Multiple database support in django

后端 未结 10 2048
囚心锁ツ
囚心锁ツ 2020-12-31 11:44

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.

Can anyone please tell

相关标签:
10条回答
  • 2020-12-31 12:32

    From Django 1.2, it will support multiple databases. See: http://docs.djangoproject.com/en/dev/topics/db/multi-db/ Version 1.2 is now in beta

    0 讨论(0)
  • 2020-12-31 12:33

    The most recent discussion I've seen on it was in the Proposal: user-friendly API for multi-database support django-developers thread, which also has an example of one way to use multiple databases using Managers in the original message.

    0 讨论(0)
  • 2020-12-31 12:35

    I think you will have to resort to "raw sql" .. kinda thing ..
    look here: 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 ..)

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

    try to take it from there ..
    P.S. I haven't really tried this or anything .. just trying to point in the general direction of what I think might solve your problem.

    0 讨论(0)
  • 2020-12-31 12:42

    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()
    
    0 讨论(0)
提交回复
热议问题