This repository has been archived on 2026-10-23. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
2017-01-11 22:14:52 +01:00

37 lines
1.2 KiB
Python

def is_routed_model(model):
return hasattr(model, 'connection_name')
class Router(object):
"""
A router to point database operations on models to the right db.
Using the attribute connection_name referring to the
DATABASES-label in settings.py.
Ref: https://docs.djangoproject.com/en/dev/topics/db/multi-db/
"""
def allow_syncdb(self, db, model):
"""Do not create tables on the default db for models on $./manage.py sync,
but this function maybe used know which db a model is on"""
if is_routed_model(model):
return db == model.connection_name
return db == 'default'
def allow_migrate(self, db, model):
if is_routed_model(model):
return db == model.connection_name
return None
def db_for_read(self, model, **hints):
"""Point all operations on models connection_name to db=connection_name"""
if is_routed_model(model):
return model.connection_name
return None
def db_for_write(self, model, **hints):
"""Point all operations on models connection_name to db=connection_name"""
if is_routed_model(model):
return model.connection_name
return None