1๐
โ
If you connect to databases inside a view, you lose on the ORM provided by django. You will have to look after the security, connections and other issues all by yourself. However, if you are so adamant on doing it, you can use mysql-connector. Try this link.
Also, you need to remove the database settings for this DB from your settings file. Only keep the primary DB connection settings.
Controlling DB connections from settings(Beware this is a hack):
In your settings.py
file, make the following changes.
import mysql.connector
try:
cnx = mysql.connector.connect(user='user', password='password',
host='your_settings',
database='your_db_name')
cnx.close()
DATABASES['custom_db'] = {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'your_db_name',
'USER': 'user',
'PASSWORD': 'password',
'HOST': 'ip_where_hosted',
'PORT': '',
}
except:
pass
๐คAnimesh Sharma
Source:stackexchange.com