【问题标题】:Single django app to use multiple sqlite3 files for database单个 django 应用程序为数据库使用多个 sqlite3 文件
【发布时间】:2015-07-02 15:19:16
【问题描述】:

我的 django 应用程序中有两个模型,我希望它们的表/数据库存储在单独的 db/sqlite3 文件中,而不是默认的“db.sqlite3”文件中。



例如:
我的 models.py 有两个类 TrainBus,我希望它们存储在 train.dbbus.db

【问题讨论】:

  • 我发现的相关问题是:(stackoverflow.com/questions/16573108/…) 但是,我需要一个 example 来说明 models.py 中的内容,settings.py 和我需要编写的任何其他文件
  • 将它们存储在单独文件中的原因是什么?你知道limitations of using multiple databases吗?
  • 我知道这一点。我真正想做的是创建存档文件。就像我在每个月底在我的程序中生成报告时一样,我希望将表数据存储在一个单独的文件中,以便在将数据写入文件时出现某种损坏。我不会丢掉旧的。将所有内容保存到一个文件会使其变得庞大且易受攻击。
  • 用多个数据库配置数据库设置是否足够,具有不同的“名称”值,然后创建一个依赖于模型名称的路由的数据库路由器? the docs 中的示例应该可以帮助您了解大部分情况。

标签: python django django-models sqlite


【解决方案1】:

当然,您始终可以只使用Train.objects.using('train') 进行调用,这样可以选择正确的数据库(假设您在settings.py 中定义了一个名为train 的数据库。

如果您不想这样做,我遇到了类似的问题,我根据您的情况调整了我的解决方案。它部分基于this blog article,数据库路由器的Django 文档是here

使用此解决方案,您当前的数据库不会受到影响,但您当前的数据也不会转移到新的数据库中。根据您的 Django 版本,您需要包含allow_syncdb 或正确版本的allow_migrate

在settings.py中:

DATABASES = {
     'default': {
         'NAME': 'db.sqlite3',
         'ENGINE': 'django.db.backends.sqlite3',
     },
     'train': {
         'NAME': 'train.db',
         'ENGINE': 'django.db.backends.sqlite3',
     },
     'bus': {
         'NAME': 'bus.db',
         'ENGINE': 'django.db.backends.sqlite3',
     },
}


DATABASE_ROUTERS = [ 'yourapp.DatabaseAppsRouter']

DATABASE_APPS_MAPPING = {'train': 'train', 'bus': 'bus'}

在名为 database_router.py 的新文件中:

from django.conf import settings

class DatabaseAppsRouter(object):
    """
    A router to control all database operations on models for different
    databases.

    In case an app is not set in settings.DATABASE_APPS_MAPPING, the router
    will fallback to the `default` database.

    Settings example:

    DATABASE_APPS_MAPPING = {'model_name1': 'db1', 'model_name2': 'db2'}

    """

    def db_for_read(self, model, **hints):
        """Point all read operations to the specific database."""
        return settings.DATABASE_APPS_MAPPING.get(model._meta.model_name, None)

    def db_for_write(self, model, **hints):
        """Point all write operations to the specific database."""
        return settings.DATABASE_APPS_MAPPING.get(model._meta.model_name, None)

    def allow_relation(self, obj1, obj2, **hints):
        """Have no opinion on whether the relation should be allowed."""
        return None

    def allow_syncdb(self, db, model): # if using Django version <= 1.6
        """Have no opinion on whether the model should be synchronized with the db. """
        return None

    def allow_migrate(db, model): # if using Django version 1.7
        """Have no opinion on whether migration operation is allowed to run. """
        return None

    def allow_migrate(db, app_label, model_name=None, **hints): # if using Django version 1.8
        """Have no opinion on whether migration operation is allowed to run. """
        return None

(编辑:这也是 Joey Wilhelm 建议的)

【讨论】:

  • 感谢您为代码示例付出的努力。 :) 这正是我的想法。
猜你喜欢
  • 2011-11-06
  • 1970-01-01
  • 2017-12-11
  • 2017-03-04
  • 1970-01-01
  • 2015-10-02
  • 2019-11-16
相关资源
最近更新 更多