Django项目开发中,ORM默认的连接对对象对象都是settings.DATABASES['default']
如果一个Django项目中有多个应用,分别需要使用不同的数据库,该如何配置呢?

第一步:在settings中配置多个数据库对象:

DATABASES = {
    'default': {},
    'common': {
        # 'ENGINE': 'django.db.backends.sqlite3',
        # 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
        'ENGINE': 'django.db.backends.mysql',
        'HOST': '127.0.0.1',
        'PORT': 3306,
        'NAME': 'common',
        'USER': 'root',
        'PASSWORD': '123456.com'
    },
    'bug_trace': {
        # 'ENGINE': 'django.db.backends.sqlite3',
        # 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
        'ENGINE': 'django.db.backends.mysql',
        'HOST': '127.0.0.1',
        'PORT': 3306,
        'NAME': 'bug_trace',
        'USER': 'root',
        'PASSWORD': '123456.com'
    },
    'student_v2': {
        # 'ENGINE': 'django.db.backends.sqlite3',
        # 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
        'ENGINE': 'django.db.backends.mysql',
        'HOST': '127.0.0.1',
        'PORT': 3306,
        'NAME': 'student_v2',
        'USER': 'root',
        'PASSWORD': '123456.com'
    }
}
  • 在这里我定义了4个数据库,分别是:
    • default,空数据库,即使用不到,也需要定义一个空对象。
    • common,公用数据库,存放项目公用的信息(用户,session等)。
    • bug_trace,代码管理的应用数据库。
    • student_v2,学生管理的应用数据库。

 

方法一:使用数据库路由

第二步:在settings中配置多个数据库路由:

DATABASE_ROUTERS = ['common.database_router.DatabaseAppsRouter']
DATABASE_APPS_MAPPING = {
    'auth': 'common',  # django内置数据库
    'admin': 'common',  # django内置数据库
    'contenttypes': 'common',  # django内置数据库
    'sessions': 'common',  # django内置数据库
    'messages': 'common',  # django内置数据库
    'staticfiles': 'common',  # django内置数据库
    'bug_trace': 'bug_trace',
    'student_v2': 'student_v2',
}
  • DATABASE_ROUTERS,表示路由对象:
    • common.database_router.DatabaseAppsRouter代表common目录下database_router.py文件中的DatabaseAppsRouter类。
  • DATABASE_APPS_MAPPING表示应用与数据库的映射关系,需要一一对应,我这里将一些公用组件全部对应到common数据库中。

第三步:在对应的目录下创建类文件:

# -*- coding:utf-8 -*-


from django.conf import settings
DATABASE_MAPPING = settings.DATABASE_APPS_MAPPING


class DatabaseAppsRouter(object):

    def db_for_read(self, model, **hints):
        """"Point all read operations to the specific database."""
        if model._meta.app_label in DATABASE_MAPPING:
            return DATABASE_MAPPING[model._meta.app_label]
        return None

    def db_for_write(self, model, **hints):
        """Point all write operations to the specific database."""
        if model._meta.app_label in DATABASE_MAPPING:
            return DATABASE_MAPPING[model._meta.app_label]
        return None

    def allow_relation(self, obj1, obj2, **hints):
        """Allow any relation between apps that use the same database."""
        db_obj1 = DATABASE_MAPPING.get(obj1._meta.app_label)
        db_obj2 = DATABASE_MAPPING.get(obj2._meta.app_label)
        if db_obj1 and db_obj2:
            if db_obj1 == db_obj2:
                return True
            else:
                return False
        return None

    def allow_syncdb(self, db, model):
        """Make sure that apps only appear in the related database."""

        if db in DATABASE_MAPPING.values():
            return DATABASE_MAPPING.get(model._meta.app_label) == db
        elif model._meta.app_label in DATABASE_MAPPING:
            return False
        return None

    def allow_migrate(self, db, app_label, model_name=None, **hints):
        db, app_label, model_name, hints
        if db in DATABASE_MAPPING.values():
            return DATABASE_MAPPING.get(app_label) == db
        elif app_label in DATABASE_MAPPING:
            return False
        return None

第四步:数据库迁移:

python3 manage.py makemigrations
# 仅迁移common数据库对应的应用
python3 manage.py migrate --database='common'
# 仅迁移student_v2数据库对应的应用
python3 manage.py migrate --database='student_v2'
# 仅迁移bug_trace2数据库对应的应用
python3 manage.py migrate --database='bug_trace'

第五步:正常使用ORM语句:

from django.http import HttpResponse, HttpResponseRedirect
from student_v2.models import Student


# 查询接口
def student_retrieve(request, action=None, sno=None):

    # 获取全部用户
    student = list(Student.objects.all().values())
    return HttpResponse(str(student))

 

方法二:手动指定数据库:

这种方式适合于不需要进行数据迁移的应用,即意味着通过运维直接建表、修改表结构等,而不是通过代码完成。

第二步:使用ORM语句时使用using函数:

from django.http import HttpResponse, HttpResponseRedirect
from student_v2.models import Student


# 查询接口
def student_retrieve(request, action=None, sno=None):

    # 获取全部用户
    student = list(Student.objects.using('student_v2').all().values())
    return HttpResponse(str(student))
  • using('student_v2'),指定使用student_v2数据库进行查询。