使用多个数据库进行Django1.8测试时出错

2024-09-30 10:35:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在将django1.8项目从单个数据库设置移到writer/reader设置。我遇到了Django bug 23718中描述的问题,但是所描述的解决方法似乎没有帮助。在

有人遇到过类似的问题吗?相关代码段如下:

路由器:

class DatabaseRouter(object):

    """Router to handle separation of reads and writes."""

    def db_for_read(self, model, **hints):
        """Reads go to a read replica."""
        return 'read'

    def db_for_write(self, model, **hints):
        """Writes always go to default."""
        return 'default'

    def allow_relation(self, obj1, obj2, **hints):
        """Allow relations bet/n objects in the same DB cluster."""
        db_list = ('default', 'read')
        if obj1._state.db in db_list and obj2._state.db in db_list:
            return True
        return None

    def allow_migrate(self, db, app_label, model_name=None, **hints):
        """All models end up in this pool."""
        return True

相关数据库设置

^{pr2}$

解决复制测试用例:

class ReplicationTestCase(TestCase):

    @classmethod
    def setUpClass(cls):
        super(ReplicationTestCase, cls).setUpClass()
        connections['read']._orig_cursor = connections['read'].cursor
        connections['read'].cursor = connections['default'].cursor

    @classmethod
    def tearDownClass(cls):
        connections['read'].cursor = connections['read']._orig_cursor
        super(ReplicationTestCase, cls).tearDownClass()

有什么爆料吗?我很乐意从我们的测试环境中提供stacktraces,如果那也有用的话。谢谢!在


Tags: toinselfdefaultreaddbmodelreturn

热门问题