[Answered ]-Django models with external DBs

2👍

An alternative is to use SQLAlchemy for the external database. It can use reflection to generate the SQLAlchemy-equivalent of django models during runtime.

It still won’t be without issues. If your code depends on a certain column, it would still break if that column is removed or changed in an incompatible way. However, it will add a bit more flexibility to your database interactions, e.g. a Django model would definitely break if an int column is changed to a varchar column, but using database reflection, it will only break if your code depends on the fact that it is an int. If you simply display the data or something, it will remain fully functional. However, there is always a chance that a change doesn’t break the system, but causes unexpected behaviour.

If, like Benjamin said, the external system has an API, that would be the preferred choice.

👤knbk

0👍

I suggest you to read about inspectdb and database routers. It’s possible to use the django ORM to manipulate a external DB.
https://docs.djangoproject.com/en/1.7/ref/django-admin/#inspectdb

👤Wille

0👍

I would create the minimal django models on the external databases => those that interact with your code:

Several outcomes to this

  1. If parts of the database you’re not interested in change, it won’t have an impact on your app.
  2. If the external models your using change, you probably want to be aware of that as quickly as possible (your app is likely to break in that case too).
  3. All the relational databases queries in your code are handled by the same ORM.

Leave a comment