Is it possible to have separate SQLite databases in a single Django project?

I was considering creating a separate SQLite database for specific applications in a Django project.
However, I did not want to use SQLite direct access, if possible. Access to this ORM-style Django database would be ideal.
Is it possible?

Thanks.

+4
source share
4 answers

Yes - a low-level API for this is in place, at the moment there is no convenient high-level API. These are quotes from James Bennett (Django Release Manager) on reddit programming :

He has been there - in an extremely low-level API for those looking at the codebase - for several months (each QuerySet supported by Query , which in turn takes a database connection as an argument). There is no high level documented API for it, but I know people who already do and have done things like scripts with multiple DB / sharding.

... this is not necessarily something that requires a great review; __init__() method of the QuerySet takes an argument from the Query keyword, which must be an instance of django.db.models.sql.Query . The __init__() Query method, in turn, takes an argument to the connection keyword, which must be an instance (the base subclass of your database) django.db.backends.BaseDatabaseWrapper .

From there it is pretty easy; you could, for example, override get_query_set() in the manager to always return a QuerySet with the connection you need, or configure things like ball logic to figure out which database to use based on the incoming query parameters, etc. etc..

+5
source

At present, no - each project uses one database, and each application must exist inside it. If you want to have an application-specific database, you cannot do this through Django ORM. See the Django wiki page in Multiple Database Support .

+2
source

This is not yet possible, but there is some talk about it in the wiki, Support for multiple databases in Django . It was also raised during a keyword about the future of Django at DjangoCon 2008 and made one of the problems with a higher priority.

0
source

Source: https://habr.com/ru/post/1277450/


All Articles