Django应用程序模型在不同的数据库上

时间:2015-02-26 16:21:16

标签: python django orm

我有两个django应用,在服务器Main上调用A,在服务器Tasker上调用BMain响应用户请求并执行许多可以快速完成的事情。 另一方面,Tasker只有一些用于记录和芹菜任务的模型。

在服务器A上,'{1}}中不包含'tasker',因为我在那里不需要它,而在服务器INSTALLED_APPS上,它是。{/ p>

django's documentation开始,我创建了一个路由器并定义了Bdb_for_read

db_for_write

在服务器B上,class ModelsRouter(object): """ Logging models are on local, but updated models are on another server """ def db_for_read(self, model, **hints): if model._meta.app_label == 'tasker': return 'tasker' return None def db_for_write(self, model, **hints): if model._meta.app_label == 'tasker': return 'tasker' return None 设置包含两个键:

  • DATABASES指向服务器A
  • default指向localhost

我遇到的问题是,当我运行tasker时,manage.py migrate的模型是在服务器A上创建的。 如何在服务器B上设置项目以了解以下内容:   - tasker app的模型在服务器A上   - main的模型在服务器B上(也是本地主机)?

1 个答案:

答案 0 :(得分:0)

我设法通过以下方式解决问题:

  • 如果模型不是来自app main
  • ,我修改了ModelsRouter以使用tasker数据库配置
  • 在我部署tasker的服务器上,我修改了DATABASES,以便default指向localhostmain指向其他服务器main 1}}居住

在服务器B上,我运行manage.py migrate tasker,因为该数据库中不需要其他模型。 它现在正在运作:

  • 日志记录在服务器B上的表中完成
  • 在其他服务器上执行更新

运行manage.py migrate tasker时遇到的问题是:

RuntimeError: Error creating new content types. Please make sure contenttypes is migrated before trying to migrate apps individually.

但我会管理它。