如何在Django 1.4中使用DB路由器 [英] How to use DB router in Django 1.4

查看:76
本文介绍了如何在Django 1.4中使用DB路由器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试将Django 1.4.3设置为使用多个数据库,但是我一生都无法使其正常运行。我阅读了SO的文档和帖子,并执行了以下操作:



1)中添加第二个数据库配置settings.py ,如下所示:

  DATABASES = {
'default':{
'ENGINE':'django.db.backends.sqlite3',
'NAME':'/tmp/django.db',
'USER':'',
'PASSWORD':'',
'HOST':'',
'PORT':'',
},
'db1':{
'ENGINE ':'django.db.backends.mysql',
'NAME':'db1',
'USER':'fake',
'PASSWORD':'fake',
'HOST':'fake.host.com',
'PORT':'3306',
},
}

2)创建 routers.py 并定义数据库路由器,如下所示

(注意:根据SO的帖子,如果您在 models.py 中定义一个数据库路由器,则该路由器将无法工作)

  class DBRouter(object):
def db_for _read(自我,模型,**提示):
返回'db1'

def db_for_write(自我,模型,**提示):
返回'db1'

def allow_syncdb(self,db,model):
返回'db1'

3)将以下行添加到 settings.py

(注意:根据SO帖子,这些行必须在数据库配置之后



来自django.db导入连接
DATABASE_ROUTERS = ['fakeproject.routers.DBRouter',]

这是错误的。不要在此处放置django.db导入连接中的 ,因为它会阻止路由器注册



我的症状:

显然,我所有的呼叫都通过默认数据库路由。下面的详细信息:




  • 两个DB设置均有效(我可以执行 manage.py indpectdb --database db1 成功


  • DATABASE_ROUTERS 设置不会产生任何抱怨(即使我


  • 当我尝试通过 manage访问对象时,将错误的路径放置到DB路由器,甚至是非字符串对象) .py shell ,我可以执行 MyModel.objects.all(),但是当我实际尝试对其进行迭代时,会被告知没有这样的表。默认数据库没有该表,但是当我使用 inspectdb db1 default 之间交换数据库配置,则可以访问该对象没问题。




任何帮助都值得赞赏!

解决方案

我发现第3步中的语句 导入连接
w作为防止DB路由器被注册。当我删除此语句时,路由器已注册,并且东西开始按预期工作。


I've been trying to setup Django 1.4.3 to use multiple DBs, but for the life of me I can't make it to work. I read the documentation and posts on SO, and did the following:

1) Add a second DB configuration in settings.py, as follows:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': '/tmp/django.db',
        'USER': '',
        'PASSWORD': '',
        'HOST': '',
        'PORT': '',
    },
    'db1' : {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'db1',
        'USER': 'fake',
        'PASSWORD': 'fake',
        'HOST': 'fake.host.com',
        'PORT': '3306',
    },
}

2) Create a routers.py and define a DB router as follows
(Note: According to SO posts, if you define a DB router in models.py, the router will not work)

class DBRouter(object):
    def db_for_read(self, model, **hints):
        return 'db1'

    def db_for_write(self, model, **hints):
        return 'db1'

    def allow_syncdb(self, db, model):
        return 'db1'

3) Add the following lines to settings.py
(Note: According to SO posts, these lines must be AFTER the DATABASES configurations

from django.db import connections
DATABASE_ROUTERS = ['fakeproject.routers.DBRouter',]

This was wrong. Do NOT put from django.db import connections here as it prevents the router to be registered

My symptoms:
Apparently, all my calls are routed through the default DB. Details below:

  • Both DB settings work (I can perform manage.py indpectdb --database db1 successfully

  • DATABASE_ROUTERS setting does not generate any complains (even if I put a wrong path to the DB router, or even a non-string object)

  • When I attempt to access my objects via manage.py shell, I can do MyModel.objects.all() but when I actually try to iterate it, I'm told no such table. The default DB does not have that table, but 'db1' clearly has it as I generated the model by using inspectdb on it. As a proof, if I swap the DB configuration between db1 and default, I can access the object without problems.

Any help highly appreciated!

解决方案

I found out that the statement in step 3 "from django.db import connections " was preventing the DB router to be registered. When I removed this statement, the router was registered and stuff started to work as expected.

这篇关于如何在Django 1.4中使用DB路由器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆