Django Multiple Databases - One not always available

时光总嘲笑我的痴心妄想 提交于 2019-12-21 05:11:24

问题


I am developing a Django application which will use multiple database backends. I would like to put an sqlite database on the machine running the django application, and sync to a remote mysql database. The tricky part is that this machine running the application will not always have an internet connection, so the mysql database is not always availalble. There will be multiple machines running the application, each with it's own local sqlite DB, but all using the same remote mysql DB.

I haven't written the code yet, but here is what I have in mind. Every time I run an insert or update I would like to write it to both databases, unless the remote DB is unavailable, in which case I will save the sql statement in a table on the local database to run when the remote DB is available.

Can this be done with database routers, or do I need to manually implement this with each db statement?

Note on PK: Not directly related, but sure to be asked. The primary key will be generated locally on each machine. In the mysql DB there will be a field for this key, and a field with a unique identifier for each instance of the application, which together will provide a unique key.


回答1:


Suppose you have a model called Blog then you can use the following to store it locally and remotely (assuming that you have configured access to the remote db).

blog = Blog('test') 
blog.save() #Assuming that sqlite is the default db
try:
    blog.save(using='mysql')
except NoInternetConnection:
    pass

Make sure you have defined and configured 'mysql' in settings.py and that you handle the cases when there is no Internet connection.

Side note: I am not quite sure why you actually would want to do this. If this is for backup purposes then I would use standard backup procedures. For more information about using multiple databases see: http://docs.djangoproject.com/en/dev/topics/db/multi-db/#using-raw-cursors-with-multiple-databases




回答2:


I took DrDee's code and attached it to the post_save signal (+1 for the help).

@receiver(models.signals.post_save) #new signal decorator in Django 1.3
def save_to_remote(sender,instance,using,**kwargs):
    if using == 'default' and instance.__module__ == 'mymodel.myapp.models':
        try:
            instance.save(using='remote')
        except:
            pending_instance=Pending_Remote(
                                            pk_default=instance.pk,
                                            model_default=instance.__class__.__name__
                                            )
            pending_instance.save()

This also saves a record of what was not saved to the remote database. Note that the model Pending_Remote must not be in 'myapp'.



来源:https://stackoverflow.com/questions/4945295/django-multiple-databases-one-not-always-available

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!