2015-03-02 63 views
0

我试图建立在Django一个路由器,但它不工作,它只是似乎被一起忽略,我已经添加上设置DATABASE_ROUTERS,并建立了数据库路由器,以及:Django的忽略路由器设置

settings.py中

DATABASE_ROUTERS = ['misc.routers.MasterSlaveRouter',] 

routers.py

class MasterSlaveRouter(object): 
    def db_for_read(self, model, **hints): 
     print "Router" 
     return 'dbname' 
    def db_for_write(self, model, **hints): 
     return None 
    def allow_relation(self, obj1, obj2, **hints): 
     return None 
    def allow_migrate(self, db, model): 
     return None 

但它只是似乎并不

轮廓被用来

misc/ 
- __init__.py 
- settings.py 
- routers.py 
- urls.py 

otherapp/ 
- __init__.py 
- views.py 
.... 

杂项是主项目名称

+0

请在此处发布您的代码,而不是链接到pasebin。 – xnx 2015-03-02 12:20:40

+0

在'routers'目录中有'__init __。py'文件吗? – Selcuk 2015-03-02 12:23:23

+0

@Selcuk路由器只是文件的名称,它不是它自己的文件夹,routers.py与settings.py文件夹在同一个文件夹中 – ShinySides 2015-03-02 12:26:10

回答

0

我有类似的东西,通过if语句添加传开它与其他返回无:

def db_for_read(self, model, **hints) : 
    if model._meta.app_label == 'your_app_name' : 
     return 'dbname' 
    else : 
     return None 

不知道为什么它需要这个,但它解决了我的问题。可能值得一试。