简体   繁体   English

django settings.py和多数据库帮助

[英]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. 我正在使用django1.3,想将我的数据放入1000个数据库中,例如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. 我的意思是,当我使用manage.py syncdb --database=myapp_idmod_0 ,它不创建任何表。 Why?How can I make it works? 为什么?如何使它起作用?

For a start, I can't imagine what possible use you could have for 1000 databases. 首先,我无法想象您可能对1000个数据库有什么用。 Unless you are Facebook, which I doubt. 除非您是Facebook,否则我对此表示怀疑。

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__ ? 最后,什么是这一切的摆弄__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

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

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