Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django: dynamic database file

In my Django project I have a dependency for a third party application that produces SQLite cache files in various directories with a known schema.

I'd like to use Django models to access those databases, but obviously I cannot use a static DATABASES setup.

How can I dynamically open a SQLite database on an arbitrary path?

EDIT

As Byron Ruth pointed out, the solution is to use the django.db.connections in conjunction with the using function in the QuerySet.

like image 660
Constantinius Avatar asked Jan 10 '13 09:01

Constantinius


People also ask

What is the database file in Django?

The file is database file where all the data that you will be generating will be stored. It is a local file as Django is a server-side framework and it treats your computer as the host when you actually run the server in command line/terminal.

Which DB works best with Django?

Django officially supports the following databases: PostgreSQL. MariaDB. MySQL.

Can Django use existing database?

Django is best suited for so-called green-field development – that is, starting projects from scratch, as if you were constructing a building on a fresh field of green grass. But despite the fact that Django favors from-scratch projects, it's possible to integrate the framework into legacy databases and applications.

Does Django automatically create database?

Django doesn't create databases for you automatically. You have to do this yourself manually. This is a simple package that creates your database for you automatically, if necessary, when you run migrate for the first time.


3 Answers

The django.db.connections is a simple wrapper around DATABASES defined in your settings. The wrapper class is here: django.db.utils#L137-L227

from django.db import connections

# Add connection information dynamically..
connections.databases['new-alias'] = { ... }
# Ensure the remaining default connection information is defined.
# EDIT: this is actually performed for you in the wrapper class __getitem__
# method.. although it may be good to do it when being initially setup to
# prevent runtime errors later.
# connections.databases.ensure_defaults('new-alias')

# Use the new connection
conn = connections['new-alias']
like image 143
Byron Ruth Avatar answered Oct 15 '22 02:10

Byron Ruth


You can register database in DATABASES settings.

from your_project import settings
database_id = "unqique_name"
new_database = {}
new_database["id"] = database_id
new_database['ENGINE'] = 'django.db.backends.sqlite3'
new_database['NAME'] = '/project/data/db_%s.sql' % database_id
new_database['USER'] = ''
new_database['PASSWORD'] = ''
new_database['HOST'] = ''
new_database['PORT'] = ''
settings.DATABASES[database_id] = new_database

You can but you shouldn't.

like image 39
baklarz2048 Avatar answered Oct 15 '22 03:10

baklarz2048


Assuming the only engine used is SQLite and the location of the (only) database file is what varies, provide a callable to the NAME:

def get_db_loc():
    # code to determine filesystem location of database
    return location

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': get_db_loc(),
        # More config goes here
    }
}
like image 1
stellarchariot Avatar answered Oct 15 '22 03:10

stellarchariot