gpt4 book ai didi

database - 是否可以在同一个 Django 项目中使用不同的 SQLite 数据库?

转载 作者:IT王子 更新时间:2023-10-29 06:31:02 26 4
gpt4 key购买 nike

我正在考虑为 Django 项目中的某些应用程序创建一个单独的 SQLite 数据库。
但是,如果可能的话,我不想使用直接的 SQLite 访问。对这些数据库的 Django 风格 ORM 访问将是理想的。
这可能吗?

谢谢。

最佳答案

是的 - 此类的低级API已经到位,目前它只是缺少方便的高级API。这些引述来自 James Bennett (Django's release manager) on programming reddit :

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.

关于database - 是否可以在同一个 Django 项目中使用不同的 SQLite 数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/168218/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com