I'm following the instructions on how to handle multiple databases within one Django project from here topics/db/multi-db
I've created the two routers required. They are saved as ./database_routers/discourse.py and ./database_routers/wordpress.py
The contents of ./database_routers/discourse.py is
class DiscourseRouter:
"""
A router to control all database operations on models in the
discourse application.
"""
def db_for_read(self, model, **hints):
"""
Attempts to read discourse models go to discourse.
"""
if model._meta.app_label == 'discourse':
return 'discourse'
return None
def db_for_write(self, model, **hints):
"""
Attempts to write discourse models go to discourse.
"""
if model._meta.app_label == 'discourse':
return 'discourse'
return None
def allow_relation(self, obj1, obj2, **hints):
"""
Allow relations if a model in the discourse app is involved.
"""
if obj1._meta.app_label == 'discourse' or \
obj2._meta.app_label == 'discourse':
return True
return None
def allow_migrate(self, db, app_label, model_name=None, **hints):
"""
Make sure the discourse app only appears in the 'discourse'
database.
"""
if app_label == 'discourse':
return db == 'discourse'
return None
The contents of ./database_routers/wordpress.py is
class WordpressRouter:
"""
A router to control all database operations on models in the
wordpress application.
"""
def db_for_read(self, model, **hints):
"""
Attempts to read wordpress models go to wordpress.
"""
if model._meta.app_label == 'wordpress':
return 'wordpress'
return None
def db_for_write(self, model, **hints):
"""
Attempts to write wordpress models go to wordpress.
"""
if model._meta.app_label == 'wordpress':
return 'wordpress'
return None
def allow_relation(self, obj1, obj2, **hints):
"""
Allow relations if a model in the wordpress app is involved.
"""
if obj1._meta.app_label == 'wordpress' or \
obj2._meta.app_label == 'wordpress':
return True
return None
def allow_migrate(self, db, app_label, model_name=None, **hints):
"""
Make sure the wordpress app only appears in the 'wordpress'
database.
"""
if app_label == 'wordpress':
return db == 'wordpress'
return None
I created an empty ./database_routers/__init__.py
file
The database router settings in api/settings I've set to
DATABASE_ROUTERS = ['database_routers.DiscourseRouter', 'database_routers.WordpressRouter']
When I attempt to look at the project using shell plus I with
./manage.py shell_plus
I get
ImportError: Module "database_routers" does not define a "DiscourseRouter" attribute/class
How do you add database routers to a Django project such that python recognises the path directory_name.ClassName?
database_routers
module /class in problem statement. – Pangolin__init__.py
file in your database_routers directory, notinit.py
. – Burma__init__.py
file in database_routers directory, it is displaying as init.py for some reason. – ParenthesisDATABASE_ROUTERS=[database_routers.db_for_read(DiscourseRouter),database_routers.db_for_read(WordpressRouter)]
change this – Pangolin'database_routers.DiscourseRouter'
to'database_routers.discourse.DiscourseRouter'
. Same for the other router. – Burma__init__.py
is missing or import path is wrong. :) – Burma