Jerry Meng
Jerry Meng

Reputation: 1506

django alter settings for one app

Well, I have a django project which works fine now.

I'd like to add a new app to it, in which I need to access multiple databases.

I know Django support multiple databases settings and know how to configure it. This is not the problem.

The issue is that, in the 90% of my project components, I don't need to maintain a multiple databases settings. The only usage for the second databases is in the new added app.

So I tried to alter the settings by calling:

django.conf.settings.configure(DATABASES = {....})

in the new app. And django said:

RuntimeError: Settings already configured.

Which makes sense, since I have the origin settings file and set the DJANGO_SETTINGS_MODULE up.

So I question is that what should be a good approach in this case.

  1. I don't want to discard DJANGO_SETTINGS_MODULE variable.
  2. I try to not include second database in the setting file initially, since the new app is also an independent module which should work independently outside the django project. So I want to have the similar config in new app to setup the database config.

Does anyone has any idea about this?

Thanks in advance!

Upvotes: 0

Views: 575

Answers (1)

Gabriel Pichot
Gabriel Pichot

Reputation: 2493

Actually, I have the same issue in a current project. As you I have a totally independent app which uses an another database, and I could have other apps which could have the same behaviour.

The thing that I have done is to create a dir apps where I store my apps and then I add this at the end of my settings.py file :

DATABASE_ROUTERS = ['myproject.routers.MultiDBRouter']
import os
APPS_DIR = os.path.join(PROJECT_ROOT, 'apps')
for app_name in os.listdir(APPS_DIR):
  print '\nLooking for 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 :'
      for key, value in app.settings.DATABASES.iteritems():
        if DATABASES.has_key(key):
          print '    Can not add %s database config, because it already exists' % key
        else:
          DATABASES[key] = value
          DATABASES[key]['APPS'] = [app_name]
          print '    Added %s database config' % key

It will automatically look after settings.py file in all the apps/myapp/ directories. If it finds a new DATABASES variable in a app/myapp/settings.py file, it will add the other database configurations to your DATABASES variable (the true one).

I have also created a router to do not have to use the using command (the MultiDBRouter).

And then I add a settings.py file in all the app which requires another database :

DATABASES = {
  'db': {
     'ENGINE': 'django.db.backends.mysql',
     'NAME': 'database',
     'USER': 'username',
     'PASSWORD': 'mysecretpassword',
   }
 }

Upvotes: 2

Related Questions