django alter settings for one app -
well, have django project works fine now.
i'd add new app it, in need access multiple databases.
i know django support multiple databases settings , know how configure it. not problem.
the issue that, in 90% of project components, don't need maintain multiple databases settings. usage second databases in new added app.
so tried alter settings calling:
django.conf.settings.configure(databases = {....})
in new app. , django said:
runtimeerror: settings configured.
which makes sense, since have origin settings file , set django_settings_module up.
so question should approach in case.
- i don't want discard django_settings_module variable.
- i try not include second database in setting file initially, since new app independent module should work independently outside django project. want have similar config in new app setup database config.
does has idea this?
thanks in advance!
actually, have same issue in current project. have totally independent app uses database, , have other apps have same behaviour.
the thing have done create dir apps store apps , add @ end of settings.py file :
database_routers = ['myproject.routers.multidbrouter'] import os apps_dir = os.path.join(project_root, 'apps') app_name in os.listdir(apps_dir): print '\nlooking settings in apps/%s :' % app_name if os.path.exists(os.path.join(apps_dir, app_name, 'settings.py')): print ' settings file found...' app = __import__('%s.settings' % app_name) content = dir(app.settings) if 'databases' in content: print ' adding databases :' key, value in app.settings.databases.iteritems(): if databases.has_key(key): print ' can not add %s database config, because exists' % key else: databases[key] = value databases[key]['apps'] = [app_name] print ' added %s database config' % key
it automatically after settings.py file in apps/myapp/ directories. if finds new databases variable in app/myapp/settings.py file, add other database configurations databases variable (the true one).
i have created router not have use using command (the multidbrouter).
and add settings.py file in app requires database :
databases = { 'db': { 'engine': 'django.db.backends.mysql', 'name': 'database', 'user': 'username', 'password': 'mysecretpassword', } }
Comments
Post a Comment