问题
I have a Django (1.6) project with two databases. I have one app with one model, and multiple tables.
I want to use the database routers to set specific tables in the model to a specific database. All the documentation I have found seems to explain how to route a particular app to a particular database.
回答1:
Looks like you could use a custom router and model attribute for this.
YMMV: Haven't tested this.
https://docs.djangoproject.com/en/dev/topics/db/multi-db/#using-routers
class MyModel(models.Model):
_DATABASE = "foo"
class CustomRouter(object):
def db_for_read(self, model, **hints):
database = getattr(model, "_DATABASE", None)
return database
# repeat for db_for_write, etc.
来源:https://stackoverflow.com/questions/19905476/django-routing-to-database-based-on-model-table