Django multiple databases - can't connect to mysql server fallback to sqlite
I have a django appli开发者_开发问答caiton with multiple databases. The default database is on the local machine. There is also a remote mysql database which is used for some write operations, but it is not always up. When the server is down, mysqldb raises an OperationalError.
I would like have a local sqlite database called 'fallback' which would accept the data if the mysql server is down. I realize that this involves at try/except clause in django.db.mysql.base, but I am not quite sure where to go from there. Has anyone tried something similar? Do you have suggestions on a better way to handle this?
You could probably use Database Routers in combination with a custom base Model class that overrides the save
method. Wrap it in a try..catch
, and if the OperationalError occurs, provide some hints so your database router can determine if the fallback needs to be used.
I think this will be the cleanest way, rather than modifying the django code itself.
精彩评论