You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何让Django各应用使用独立SQLite数据库?

Django多应用独立SQLite数据库配置方案

你的数据库配置已经完成了第一步,接下来需要通过数据库路由让Django明确每个应用的模型对应哪个数据库,具体步骤如下:

1. 创建数据库路由类

在项目根目录(或任意可被Python导入的位置)新建db_routers.py文件,添加以下代码:

class AccountsRouter:
    route_app_labels = {'accounts'}

    def db_for_read(self, model, **hints):
        if model._meta.app_label == 'accounts':
            return 'accounts_db'
        return None

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

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

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


class BooksRouter:
    route_app_labels = {'books'}

    def db_for_read(self, model, **hints):
        if model._meta.app_label == 'books':
            return 'books_db'
        return None

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

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

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


class PublishersRouter:
    route_app_labels = {'publishers'}

    def db_for_read(self, model, **hints):
        if model._meta.app_label == 'publishers':
            return 'publisher_db'
        return None

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

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

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

2. 在settings.py中注册路由

找到DATABASE_ROUTERS配置项(没有则新增),添加路由类的完整路径:

DATABASE_ROUTERS = [
    '你的项目名.db_routers.AccountsRouter',
    '你的项目名.db_routers.BooksRouter',
    '你的项目名.db_routers.PublishersRouter',
]

注意将你的项目名替换为实际的项目根目录名称。

3. 执行分库迁移

每个数据库独立,需分别执行迁移命令:

  • 迁移accounts应用到accounts_db:
    python manage.py migrate --database=accounts_db accounts
    
  • 迁移books应用到books_db:
    python manage.py migrate --database=books_db books
    
  • 迁移publishers应用到publisher_db:
    python manage.py migrate --database=publisher_db publishers
    

若需初始化默认数据库(如admin后台相关表),执行常规迁移:

python manage.py migrate

4. 注意事项

  • SQLite不支持跨数据库关联查询,若模型存在跨应用关联,需调整设计或在路由的allow_relation方法中做特殊处理。
  • 运行测试时,Django会自动为每个数据库创建对应的测试库,无需额外配置。

内容的提问来源于stack exchange,提问作者Amir

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 19:22:59