django数据库同步供离线使用
我有一台主 django 服务器,用于存储数据(mysql 数据库)。
在线:我希望许多用户在他们的笔记本电脑(sqlLite DB)上同步此数据库的副本(仅必须复制增量)
离线(用户无权访问主服务器):用户可以查看和更新其本地数据库。
返回在线:用户笔记本电脑上的修改内容会同步回主 django 服务器。
我认为,由于我有两种数据库,我需要在 django 对象级别进行同步。 有 django 应用程序可以做到这一点吗? 如果没有,您将如何编写这样的功能?
I have one master django sever where the data are stored (mysql database).
Online : I would like many users to have a copy from this database synchronized (only delta's must be copied) on their laptops (sqlLite DB)
Offline (users do not have access to the master server) : users can view and update their local database.
Back to Online : what has been modified on users laptops is synchronized back to the master django server.
I think,as I have 2 kind of database, I need to synchronize at django object level.
Is there a django application doing that ?
If not, how will you procced to code such a feature ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
事实证明我正在 Django 中运行这样的系统。
这不是一个完整的答案,只是当前正在解决(大部分)问题的答案。
我们在使用这个简单的系统时遇到了很少的麻烦,而且由于内容被正确分类并且编辑器仅创建/编辑一组不重叠的类别,因此也有所帮助。
我已经与一些人讨论过这个问题,并向我提出了几种解决方案:
希望这能在某种程度上有所帮助。如果有人决定实施其中的某些内容,我很乐意听取他的意见。
Turns out that I'm running a system like this in Django.
This is not a complete answer, just the answer that is currently solving (mostly) the problem.
We have run into very little trouble with this simple system, also helped since the content is properly categorized and the editors only create/edit a non overlapped set of categories.
I have talked with this with some people, and proposed me several solutions:
Hope this helps in some way. If anyone decides to implement something of this, I'll love to hear from him.
我构建了一个 Django 应用程序来执行此操作。当在应用程序的远程/笔记本电脑版本上创建模型实例时,它们会被标记为脏并获得临时 ID。远程应用程序定期检查与主服务器的连接。当有网络连接时,即应用程序在线时,它会从主服务器获取每个新脏模型实例的永久 ID。临时 id 被替换为永久 id,然后脏实例被同步到 master。
我使用 Django REST 框架来接收和更新主服务器上的脏模型实例。
请注意,这还需要在离线计算机上运行本地 Web 服务器。为此我选择了 CherryPy。
I built a Django app which does this. When model instances are created on the remote/laptop version of the app they are marked as dirty and get a temporary id. The remote app checks regularly for connectivity to the master server. When there is a network connection, i.e. the app is online, it gets a permanent id for each new dirty model instance from the master server. The temporary ids are replaced with permanent ids and then the dirty instances are synchronized to the master.
I used the Django REST framework to receive and update the dirty model instances on the master server.
Note that this also requires running a local web server on the offline computer. I chose CherryPy for that.
好吧,我实际上不知道是否有一个 django 应用程序可以做到这一点,但我会这样进行:
为“offline_update”创建一个方法:连接到服务器的数据库,选择 id 与本地中的 id 匹配的所有对象数据库。您更新本地数据库。然后选择其余条目,并将它们添加到本地数据库,
为“online_update”创建一个方法,与相同的例程相反。
优点:易于实现(Objects.all() 获取所有内容,然后操作和更新,或直接保存)
缺点:竞争条件(如果 2 个用户更新同一个条目(不一定在同一时间)会怎样?谁拥有最新的一个?)
你基本上创建了一种“mysql-svn”来保持两个数据库的更新。
我对你的问题投+1,因为它真的很有趣。我一直通过转储数据库(通过 mysql)然后加载到本地数据库来工作。不使用 django。
Well, I actually don't know if there's a django app to do that, but I will proceed like this:
create a method for "offline_update": connection to the server's database, you select all objects whose id matches the one in the local database. you update the local database. then you select the rest of the entries, and you add them to local database
create a method for "online_update" the same routine, inverted.
PRO: easy to implement (Objects.all() gets you everything, then you manipulate and update, or save directly)
CONS: race conditions (what if 2 users update the same entry (not necessarily on the same time)? who's got the most updated one?)
you basically create a sort of "mysql-svn" to keep the 2 databases updated.
I'm voting +1 to your question, because it's really interesting. I've always worked by dumping the database (via mysql) and then loading into the local database. without using django.