Question

I have a django site and I need to access data from another database which is in another host. how do I connect it and even if i connect, how do I access that table from this database, without having the table's data in the models.py? Please help me

Was it helpful?

Solution

If you want to use the Django ORM, you can use Django's built-in multi-database support. To create the models.py file, you can use the inspectdb management command.

If you don't need the ORM, you can connect still specify the additional database in your settings, but only perform raw SQL queries. You need to get the right connection for your additional database:

from django.db import connections
cursor = connections['my_db_alias'].cursor()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top