Django多个数据库 - 正确的路由器类

时间:2018-01-24 20:47:46

标签: python django django-models

我想在我的应用程序中使用两个数据库:

  • 本地数据库
  • 外部数据库

在第一个我要保存所有django表,例如auth_group

要做到这一点,我尝试使用路由器类,但没有成功 - 它不起作用。您可以在下面找到我的代码

Django 1.11

setings文件:

DATABASE_ROUTERS = ['mainApp.models.DefaultDBRouter','subfolder.newApp.models.testDBRouter',]

models.py - 主应用程序 - 我想为此模型使用默认数据库

from __future__ import unicode_literals

from django.db import models

class list_a( models.Model ):
    region = models.CharField(max_length=50, verbose_name="Region")
    country = models.CharField(max_length=50, verbose_name="Country")

    def __unicode__(self):
        return str( self.country )

class DefaultDBRouter(object):
    """
    A router to control all database operations on models in the
    auth application.
    """

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

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

    def allow_relation(self, obj1, obj2, **hints):
        """
        Relations between objects are allowed if both objects are
        in the default.
        """
        db_list = ('default')
        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=None, **hints):
        """
        All non-micro-management models end up in this pool.
        """
        return True

models.py - 测试应用程序 - 我想为此模型使用第二个数据库

class testTable( models.Model ):

    date = models.DateField(verbose_name="Date")
    number_name = models.CharField(max_length=50, verbose_name="Number name")

    def __unicode__(self):
        return str( self.number_name )


ELIGIBLE_APPS = [
   'subfolder.newApp',
]


class testDBRouter(object):
    def db_for_read(self, model, **hints):
        print model._meta.app_label
        if model._meta.app_label in ELIGIBLE_APPS:
            return 'secondDB'
        return None

    def db_for_write(self, model, **hints):
        if model._meta.app_label in ELIGIBLE_APPS:
            return 'secondDB'
        return None

    def allow_relation(self, obj1, obj2, **hints):
        if obj1._meta.app_label in ELIGIBLE_APPS or \
                obj2._meta.app_label in ELIGIBLE_APPS:
            return True
        return None

    def allow_migrate(self, db, app_label, model_name=None, **hints):
        if app_label in ELIGIBLE_APPS:
            return db == 'secondDB'
        return None

如果我执行以下命令:

python2.7 manage.py migrate --database=secondDB

然后django从该数据库中的模型文件和标准django表创建所有表(应该只创建一个)

1 个答案:

答案 0 :(得分:1)

我使用两个db,我按以下方式执行 我正常迁移。 python manage.py migrate

 DATABASES = {


'default': {
   'ENGINE': 'django.db.backends.mysql',
   'NAME': 'xxxx',
   'USER': 'root',
   'PASSWORD': '',
   'HOST': '',
   'PORT': '',

 },
 'base2': {
       'ENGINE': 'django.db.backends.oracle',
       'NAME': 'xxxxxx',
       'USER': '',
       'PASSWORD': '',
   },

}