I have a site that has about 7 dbs (country specific instances of the site) and I have a lot of repetition in the DATABASES dict in settings, as really the only thing that changes is the DATABASE key, for each entry.
So I wanted to build the dict dynamically in settings. My code works fine and builds the same dict as I had manually entered before, but for some reason I get this error when I try to run it:
_mysql_exceptions.OperationalError: (1046, 'No database selected')
Here's the code I'm using in settings to generate the dict:
DATABASES = {}
for d in DBS:
#SITE_INSTANCE, e.g. 'dev' and DBS is a list of db names
name = '%s_%s' % (SITE_INSTANCE, d)
if not DATABASES: #first item - set up 'default'
d = 'default'
DATABASES[d] = {}
DATABASES[d]['name'] = name
DATABASES[d]['ENGINE'] = DB_ENGINE
DATABASES[d]['USER'] = DB_USERNAME
DATABASES[d]['PASSWORD'] = DB_PASSWORD
As I said the generated dict is indistinguishable from the dict I entered manually. I can't see why this wouldn't work.