【问题标题】:Multiple databases syncing in Django在 Django 中同步多个数据库
【发布时间】:2018-11-22 00:58:20
【问题描述】:

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

python manage.py migrate

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

【问题讨论】:

    标签: django database


    【解决方案1】:

    你必须像这样在 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']
    

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

    【讨论】:

    • 还有其他选择吗?这段代码看起来很神秘,对我来说没有任何意义。感谢您的帮助。
    • 如果你想使用多个数据库,我认为你必须在你的 DJango 项目中实现这一点。但是,您应该在项目开始时考虑这种事情,而不是在开发阶段。
    猜你喜欢
    • 2014-03-31
    • 2011-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-03
    • 2012-05-08
    • 1970-01-01
    相关资源
    最近更新 更多