Django中的多个数据库同步

时间:2018-06-12 15:05:08

标签: django database

我之前使用Django的SQLite默认数据库,并将其用于用户身份验证。我正在构建CMS,因此我需要存储图像。所以我在settings.py中添加了一个PostgreSQL数据库,并在models.py中为图像创建了新模型。如何将这些新更改迁移到新创建的数据库?如果我做

python manage.py migrate

新创建的模型只会存储在SQLite数据库中吗?

1 个答案:

答案 0 :(得分:0)

您必须在settings.py文件中指定数据库,如下所示:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
    'postgresql': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'myproject',
        'USER': 'myprojectuser',
        'PASSWORD': 'password',
        'HOST': 'localhost',
        'PORT': '',
    }
}

然后,在项目文件夹中,您可以创建名为的文件: routersGlobal.py

from django.conf import settings

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

    def db_for_read(self, model, **hints):
        """
        Attempts to read auth models go to auth.
        """
        app_list = ('auth', 'admin', 'contenttypes', 'sessions',)

        if model._meta.app_label in app_list:
            return 'default' #According to database name sqlite3
        return None

    def db_for_write(self, model, **hints):
        """
        Attempts to write auth models go to auth.
        """
        app_list = ('auth', 'admin', 'contenttypes', 'sessions',)
        if model._meta.app_label in app_list:
            return 'default'
        return None

    def allow_relation(self, obj1, obj2, **hints):
        """
        Allow relations if a model in the auth app is involved.
        """
        app_list = ('auth', 'admin', 'contenttypes', 'sessions',)
        if obj1._meta.app_label in app_list and obj2._meta.app_label in app_list:
            return True
        return None

    def allow_migrate(self, db, app_label, model=None, **hints):
        """
        Make sure the auth app only appears in the 'auth'
        database.
        """
        app_list = ('auth', 'admin', 'contenttypes', 'sessions',)

        if app_label in app_list:
            return db == 'default'
        return None

您可以创建另一个名为: routersLocal.py 文件的文件:

from django.conf import settings

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

    def db_for_read(self, model, **hints):
        """
        Attempts to read auth models go to auth.
        """
        app_list = ('YourNewApp',)

        if model._meta.app_label in app_list:
            return 'postgresql'
        return None

    def db_for_write(self, model, **hints):
        """
        Attempts to write auth models go to auth.
        """
        app_list = ('YourNewApp',)
        if model._meta.app_label in app_list:
            return 'postgresql'
        return None

    def allow_relation(self, obj1, obj2, **hints):
        """
        Allow relations if a model in the auth app is involved.
        """
        app_list = ('YourNewApp',)
        if obj1._meta.app_label in app_list and obj2._meta.app_label in app_list:
            return True
        return None

    def allow_migrate(self, db, app_label, model=None, **hints):
        """
        Make sure the auth app only appears in the 'auth'
        database.
        """
        app_list = ('YourNewApp',)

        if app_label in app_list:
            return db == 'postgresql'
        return None

最后在settings.py文件中,您必须指定重定向:

DATABASE_ROUTERS = ['YourProjectName.routersLocal.LocalRouter', 'YourProjectName.routersGlobal.GlobalRouter']

然后,它应该工作并应用分离迁移