From some forum I came to know that Multiple database support is added in Django at lower level, but the higher level apis are not added yet.
Can anyone please tell me how one can achieve multiple database connections in Django.
Does anyone have any idea by when Django will fully/officially support Multiple database connections.
Django's admin doesn't have any explicit support for multiple databases. If you want to provide an admin interface for a model on a database other than that specified by your router chain, you'll need to write custom ModelAdmin classes that will direct the admin to use a specific database for content.
Postgresql is the preferred database for Django applications due to its open-source nature; and it's also ideal for complex queries. To optimize queries in any Django application, we will cover the following areas to perform database optimization, namely: database indexing.
Do Django models support multiple-column primary keys? ¶ No. Only single-column primary keys are supported.
If you simply need multiple connections, you can do something like this:
from django.db import load_backend
myBackend = load_backend('postgresql_psycopg2') # or 'mysql', 'sqlite3', 'oracle'
myConnection = myBackend.DatabaseWrapper({
'DATABASE_HOST': '192.168.1.1',
'DATABASE_NAME': 'my_database',
'DATABASE_OPTIONS': {},
'DATABASE_PASSWORD': "",
'DATABASE_PORT': "",
'DATABASE_USER': "my_user",
'TIME_ZONE': "America/New_York",})
# Now we can do all the standard raw sql stuff with myConnection.
myCursor = myConnection.cursor()
myCursor.execute("SELECT COUNT(1) FROM my_table;")
myCursor.fetchone()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With