从外部mysql数据库读取数据并将其保存到Django的默认数据库中

时间:2019-10-30 06:03:06

标签: mysql django django-models

我有2个数据库连接(默认和外部)。我想从外部数据库表中读取数据并将其保存到默认数据库表中。

settings.py

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'hello',
        'USER':'root',
        'PASSWORD': '',
        'HOST': '127.0.0.1',
    },
    'external_db': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'hello_external,
        'USER':'root',
        'PASSWORD': '',
        'HOST': '127.0.0.1',
    }
}

routers.py

    """
    A router to control all database operations on models in
    the myapp2 application
    """

    def db_for_read(self, model, **hints):
        return 'external_db'

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

    def allow_syncdb(self, db, model):
        return db == 'default'

    def allow_migrate(self, db, app_label, model_name=None, **hints):
        return (db == 'default')

0 个答案:

没有答案