33๐
โ
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']
๐คByron Ruth
4๐
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.
๐คbaklarz2048
- How to clear all session variables without getting logged out
- No Such Column Error in Django App After South Migration
- Django widget override template
- Is there any list of blog engines, written in Django?
- How show personalized error with get_object_or_404
1๐
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
}
}
๐คstellarchariot
- Why "class Meta" is necessary while creating a model form?
- How should I represent a bit flags int field in django admin?
- How to display the message passed to Http404 in a custom 404 Error template in Django?
- PUT request for image upload not working in django rest
Source:stackexchange.com