简体   繁体   中英

django settings.py and multi-database help

I'm using django1.3, wanted to put my data into 1000 databases, like app_idmod_0 ~ ap_idmod_999. Now I changed my settings.

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
        'NAME': 'myapp',                      # Or path to database file if using sqlite3.
        'USER': 'root',
        'PASSWORD': 'root',
        'HOST': '',                      # Set to empty string for localhost. Not used with sqlite3.
        'PORT': '3306',                      # Set to empty string for default. Not used with sqlite3.

    },    
}
myapp = DATABASES['default']
DB_MOD = 1000
for i in xrange(0, DB_MOD):
    myapp.__setitem__('NAME', 'myapp_idmod_' + str(i))
    DATABASES.__setitem__('myapp_idmod_' + str(i), myapp)

It doesn't work. I mean when I use manage.py syncdb --database=myapp_idmod_0 , it makes no tables. Why?How can I make it works?

For a start, I can't imagine what possible use you could have for 1000 databases. Unless you are Facebook, which I doubt.

Secondly, you are setting each instance to the same dictionary, so when you change the name in the next iteration the previous name changes too. You need to copy the dictionary before changing it.

Finally, what's all this mucking about with __setitem__ ? Why not just set the dictionary value in the normal way?

for i in xrange(DB_MOD):
    db_dict = myapp.copy()
    db_dict['NAME'] = 'myapp_idmod_' % i
    DATABASES['myapp_idmod_%s' % i] = db_dict

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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