首页 文章

多个Django数据库 - 在同一个应用程序中将模型映射到数据库

提问于
浏览
1

我已经搜索了所有的解决方案,但一直无法找到任何东西 .

我有一个Django项目,一个应用程序,两个模型和两个数据库 . 我想要一个模型说话并专门同步到一个数据库 . 这就是我尝试过的:

Settings

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
        'NAME': 'database_a',                      # Or path to database file if using sqlite3.
        # The following settings are not used with sqlite3:
        'USER': 'user',
        'PASSWORD': 'xxxxxx',
        'HOST': 'localhost',                      # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP.
        'PORT': '',                      # Set to empty string for default.
    },
    'applicationb_db': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'database_b',
        'USER': 'user',
        'PASSWORD': 'xxxxxx',
        'HOST': 'localhost',
        'PORT': '',                 
    },
}
DATABASE_ROUTERS = ['fanmode4.router.ApiRouter']

Models

from django.db import models

class TestModelA(models.Model):
    testid = models.CharField(max_length=200)
    class Meta:
        db_table = 'test_model_a'

class TestModelB(models.Model):
    testid = models.CharField(max_length=200)
    class Meta:
        db_table = 'test_model_b'
        app_label = 'application_b'

Router

class ApiRouter(object):
    def db_for_read(self, model, **hints):
        if model._meta.app_label == 'application_b':
            return 'applicationb_db'
        return None

    def db_for_write(self, model, **hints):
        if model._meta.app_label == 'application_b':
            return 'applicationb_db'
        return None

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

    def allow_syncdb(self, db, model):
        if db == 'applicationb_db':
            return model._meta.app_label == 'application_b'
        elif model._meta.app_label == 'application_b':
            return False
        return None

应用程序名称是"api" . 基本上使用此设置,如果我同步数据库,它将仅在默认数据库上同步 . 如果我同步指定第二个数据库 python manage.py syncdb --database=applicationb_db 的数据库,它将不会将任何内容同步到第二个数据库 .

我只是想实现以下目标:

  • TestModelA的所有内容都转到默认数据库

  • TestModelB的所有内容都转到applicationb_db数据库

  • 其他所有内容都转到默认数据库

1 回答

  • 2

    您可以使用 model 来检查它是哪个型号而不是使用 model._meta.app_label ,而是返回适当的DB .

    您可以将路由器更新为:

    class ApiRouter(object):
        def db_for_read(self, model, **hints):
            if model == TestModelB:
                return 'applicationb_db'
            return None
    
        def db_for_write(self, model, **hints):
            if model == TestModelB:
                return 'applicationb_db'
            return None
    
        def allow_relation(self, obj1, obj2, **hints):
            if model == TestModelB:
               return True
            return None
    
        def allow_syncdb(self, db, model):
            if model == TestModelB:
                return True
            else:
                return False
            return None
    

相关问题