【问题标题】:Django multiple databases - correct router classDjango多个数据库 - 正确的路由器类
【发布时间】:2018-01-24 20:47:45
【问题描述】:

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

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

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

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

Django 1.11

设置文件:

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 表创建所有表(应该只创建一个)

【问题讨论】:

    标签: python django django-models


    【解决方案1】:

    我使用两个 db,并按以下方式进行操作 我做正常的迁移。 python manage.py 迁移

     DATABASES = {
    
    
    'default': {
       'ENGINE': 'django.db.backends.mysql',
       'NAME': 'xxxx',
       'USER': 'root',
       'PASSWORD': '',
       'HOST': '',
       'PORT': '',
    
     },
     'base2': {
           'ENGINE': 'django.db.backends.oracle',
           'NAME': 'xxxxxx',
           'USER': '',
           'PASSWORD': '',
       },
    
    }
    

    【讨论】:

      猜你喜欢
      • 2011-12-24
      • 2018-07-03
      • 2012-03-28
      • 2014-08-09
      • 2016-10-26
      • 1970-01-01
      • 2017-06-16
      • 2017-10-16
      • 1970-01-01
      相关资源
      最近更新 更多