Multiple database support in django

后端 未结 10 2047
囚心锁ツ
囚心锁ツ 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:18

    There is a "using" directive for queries,saves, and deletes

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

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

    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. 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.
    • All DB on same machine, different engines (E.g. MySQL + PostgreSQL)
    • One Master DB with N read-only slaves on different machines.
    • Sharding of tables across multiple DB servers.

    Will you need:

    • Foreign keys across DBs
    • 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. To continue to do that, but to then add in any of the above, is Not Easy (tm).

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

    Eric Florenzano wrote a very good blog post that allows you some multiple database support at: Easy MultipleDatabase Support for Django.

    It starts by creating a new custom manager that allows you to specify the database settings.

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

    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
    
    0 讨论(0)
  • 2020-12-31 12:28

    This will be in Django 1.2.

    See http://docs.djangoproject.com/en/dev/topics/db/multi-db/

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

    Eric Florenzano's approach works well if all your databases use the same engine. 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).

    If you need this to work, you should wait for Alex Gaynor's MultiDB project which is planned for Django 1.2

    0 讨论(0)
提交回复
热议问题