Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to have separate SQLite databases within the same Django project?

I was considering creating a separate SQLite database for certain apps on a Django project.
However, I did not want to use direct SQLite access if possible. Django-style ORM access to these database would be ideal.
Is this possible?

Thank you.

like image 714
Rui Vieira Avatar asked Oct 03 '08 18:10

Rui Vieira


2 Answers

Yes - the low-level API for this is in place, it's just missing a convenient high-level API at the moment. These quotes are from 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.

like image 75
Jonny Buchanan Avatar answered Sep 28 '22 15:09

Jonny Buchanan


Already supported http://docs.djangoproject.com/en/dev/topics/db/multi-db/

like image 45
pista329 Avatar answered Sep 28 '22 15:09

pista329