Reputation: 235
I am writing a Django application where I already have 1 mysql backend db configured in my settings.py.
I know we can add as many db configurations as we want, but that's hard coding
which I don't want.. rather can't possibly do as I have to ad-hockly connect to say, about 70-80 different remote machines and query and fetch the result.
I am planning to connect to those machines via their IP address
.
I am comparatively new to Django
, so I was wondering if we can somehow, make a function
which queries the machine by putting in configuration something like :
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'dbName',
'USER': 'root',
'PASSWORD': 'root',
'HOST': '',
'PORT': '3306'
}
}
So instead of DATABASES and default
, I could configure my function to change the configuration, through an Ajax call
or something!
Fortunately, every machine I have to connect to uses mysql
so no problem with that.
I looked into this mysql -python connector but not sure if I should use it as I already have MySQLDb
installed. I also have to do some raw queries
too :|
Could anyone guide me for what would be the best approach for this situation?
P.S : I have also looked at this post which discusses about connecting to remote mysql machine from local. But that's of no help for me :( :(
Upvotes: 3
Views: 5123
Reputation: 55962
I believe there are quite a few paths you can take, 3 of which are:
Add all your connections in DATABASES
using using
- which you said you don't want to do because you have so many conections
You could connect using python's mysql library. If you do this I don't think you'll get to use djangos nice ORM
Look at how django wraps connections to allow you to use their ORM. I did some quick searches about manually establishing a connection using django ORM but didn't find anything. All the answers are in the source code. I believe you can just instantiate your own connections and interact with your remote database using the ORM. I don't have time to look through it now, but everything is in their source
Upvotes: 1