Configure Django Database Routers

懵懂的女人 提交于 2019-12-11 14:45:10

问题


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

"no app named CUCMCDR"

(which there shouldnt be. CUCMCDR is the name of the db)

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?


回答1:


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'



来源:https://stackoverflow.com/questions/44009504/configure-django-database-routers

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