title: Django: Specifying Dynamic Database at Runtime
body: <p>I'm attempting to trial setting up a system in Django whereby I specify the database connection to use at runtime. I feel I may need to go as low level as possible, but want to try and work within the idioms of Django where possible, perhaps stretching it as much as can be possible.</p>
  <p>The general precise is that I have a centralised database that stores meta information Datasets - but the actual datasets are created as dyanmic models at runtime, in the database in question. I need to be able to specify which database to connect to at runtime to then extract the data back out...</p>
  <p>I have kind of the following idea:</p>
  <pre><code>db = {}
  
  db['ENGINE'] = 'django.db.backends.postgresql'
  db['OPTIONS'] = {'autocommit': True}
  db['NAME'] = my_model_db['database']
  db['PASSWORD'] = my_model_db['password']
  db['USER'] = my_model_db['user']
  db['HOST'] = my_model_db['host']
  
  logger.info(&quot;Connecting to database {db} on {host}&quot;.format(db=source_db['NAME'], host=source_db['HOST']))
  
  connections.databases['my_model_dynamic_db'] = db
  
  DynamicObj.objects.using('my_model_dynamic_db').all()
      
  </code></pre>
  <p>Has anyone achieved this? And how?</p>