0

i'm looking for a "best-practice" guide/solution to the following situation.

I have a Django project with a MySql DB which i created and manage. I have to import data, every 5 minutes, from a second (external, not managed by me) db in order to do some actions. I have read rights for the external db and all the necessary information.

I have read the django docs regarding the usage of multiple database: register the db in settings.py, migrate using the --database flag, query/access data by routing to the db (short version) and multiple question on this matter on stackoverflow.

So my plan is: Register the second database in settings.py, use inspectdb to add to the model, migrate, define a method which reads data from the external db and add it to the internal (own) db.

However I do have some questions:

  1. Do i have to register the external db if i don't manage it? (Most probably yes in order to use ORM or the cursors to access the data)

  2. How can i migrate the model if I don't manage the DB and don't have write permissions? I also don't need all the tables (around 250, but only 5 needed). (is fake migration an option worth considering? I would use inspectdb and migrate only the necessary tables.)

  3. Because I only need to retrieve data from the external db and not to write back, would it suffice to have a method that constantly gets the latest data like the second solution suggested in this answer

Any thoughts/ideas/suggestions are welcomed!

ap1393
  • 37
  • 10

1 Answers1

1

I would not use Django's ORM for it, but rather just access the DB with psycopg2 and SQL, get the columns you care about into dicts, and work with those. Otherwise any minor change to that external DB's tables may break your Django app, because the models don't match anymore. That could create more headaches than an ORM is worth.

C14L
  • 12,153
  • 4
  • 39
  • 52