在同一个Django项目中是否可以有单独的SQLite数据库?

时间:2020-03-06 15:05:23  来源:igfitidea点击:

我当时正在考虑为Django项目上的某些应用创建单独的SQLite数据库。
但是,如果可能的话,我不想使用直接SQLite访问。
Django样式的ORM访问这些数据库将是理想的。
这可能吗?

谢谢你。

解决方案

当前,每个项目都不使用一个数据库,并且每个应用程序都必须存在于其中。如果要拥有特定于应用程序的数据库,则不能通过Django ORM来实现。请参阅"多数据库支持"上的Django Wiki页面。

这还不可能,但是在Wiki上有一些讨论,Django中的"多数据库支持"。它也是在DjangoCon 2008上有关Django未来的主题演讲中提出的,并且成为优先级较高的问题之一。

是的,已经存在用于此目的的低级API,此刻它只是缺少一个方便的高级API。这些引文来自James Red编程的reddit的James Bennett(Django的发行经理):

It's been there -- in an extremely low-level API for those who look at the codebase -- for months now (every QuerySet is backed by a Query, which in turn accepts a DB connection as an argument). There isn't any high-level documented API for it, but I know people who are already doing and have been doing stuff like multiple-DB/sharding scenarios.
  
  ...it's not necessarily something that needs a big write-up; the __init__() method of QuerySet accepts a keyword argument query, which should be an instance of django.db.models.sql.Query. The __init__() method of Query, in turn, accepts a keyword argument connection, which should be an instance of (a backend-specific subclass for your DB of) django.db.backends.BaseDatabaseWrapper.
  
  From there, it's pretty easy; you could, for example, override get_query_set() on a manager to always return a QuerySet using the connection you want, or set up things like sharding logic to figure out which DB to use based on incoming query parameters, etc., etc.

已经支持的http://docs.djangoproject.com/en/dev/topics/db/multi-db/