配置Django数据库路由器 [英] Configure Django Database Routers
问题描述
我正在尝试将新的Django站点连接到旧版数据库。从我能收集的所有信息中,我需要创建一个数据库路由器。文档指的是在模型的meta部分中创建app_label。这就是路由器要匹配的内容。我还看到有人说不再支持此功能。我在这里绞尽脑汁,请帮忙!!下面是代码:
I am trying to connect a new Django site to legacy DBs. From everything I can gather I need to create a database router. The docs refer to creating an app_label in the meta section of the model. This is what the router would match to. I have also seen where people say this is not longer supported. I am racking my brain here please help!! Here is the code:
class CucRouter(object):
def db_for_read(self, model):
if model._meta.app_label == 'CUCMCDR':
return 'CUCMCDR'
return 'default'
这是路由器功能,本质上是文档中的示例。在模型中,我添加了以下行:
That is the router function and is essentially the example from the docs. In the model I added this line:
app_label = 'CUCMCDR'
这会中断服务器,并返回错误消息
This breaks the server with an error return that there is
没有名为CUCMCDR的应用程序
"no app named CUCMCDR"
(不应存在。CUCMCDR是数据库的名称)
(which there shouldnt be. CUCMCDR is the name of the db)
编辑**我正在从管理面板执行所有操作。我正在尝试仅创建一个APP。也许创建另一个更容易?
Edit** I'm doing this all from the admin panel. I am trying to create just one APP. Maybe it would be easier to create another?
推荐答案
所以我明白了。如果其他任何人都遇到了多个数据库的问题,则这是一个订购问题。您需要将第二个返回值从默认更改为无
SO I got it. If anyone else is running into this problem with multiple dbs it is an ordering issue. You need to change the second return frrom 'default' to 'None'
这篇关于配置Django数据库路由器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!