Note that there are some explanatory texts on larger screens.

plurals
  1. PODjango multi-database routing
    primarykey
    data
    text
    <p>I have been using manual db selection to cope with a project which has two seperate dbs. I have defined my databases in the settings. After some further reading it seems that database routing is actually the way to go with this. However, after reading the docs and some relevant posts here I am more confused than ever.</p> <p>In my settings I have:</p> <pre><code>DATABASES = { 'default': { .... }, 'my_db2': { .... } } DATABASE_ROUTERS = ['myapp2.models.MyDB2Router',] </code></pre> <p>I know I have to define my router class (I <strong>think</strong> in myapp2.models.py) like so:</p> <pre><code>class MyDB2Router(object): """A router to control all database operations on models in the myapp2 application""" def db_for_read(self, model, **hints): if model._meta.app_label == 'myapp2': return 'my_db2' return None def db_for_write(self, model, **hints): if model._meta.app_label == 'myapp2': return 'my_db2' return None def allow_relation(self, obj1, obj2, **hints): if obj1._meta.app_label == 'myapp2' or obj2._meta.app_label == 'myapp2': return True return None def allow_syncdb(self, db, model): if db == 'my_db2': return model._meta.app_label == 'myapp2' elif model._meta.app_label == 'myapp2': return False return None </code></pre> <p>Then what? Does each model require a meta.app_label or is that automatic? Aside from that, I still get an error: </p> <p>django.core.exceptions.ImproperlyConfigured: Error importing database router JournalRouter: "cannot import name connection</p> <p>Can anyone help me understand what is happening and what is going wrong? Any help much appreciated.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload