Django connect to a remote database

This is a very general question, however I seem to be stuck:
How do I query a remote database particularly an Oracle (or any database really) database, do I have to create a model for the database? Also why would I need to create a model for the tables in that database if the tables already exist?

See the docs on Multiple Databases for how to set up your connections. Also see Integrating Django with a legacy database.

A model is the Python/Django representation of a table. Do you need to create a model for it? No. But doing so allows you to use the ORM for writing queries, along with using all the other Django facilities for working with tables.

A model isn’t written to create tables. It’s used to provide a Python-based representation of that table, along with the code needed to convert data between the internal (Python representation) and external (database) representation.

The existence of utilities within Django to create or update the schema of a database based upon those models should not be confused with the real underlying purpose of those models. (There is also a tool, inspectdb, that will help you create models from existing tables.)

Wouldn’t using a restful api to query the legacy database much faster/easier?

Why do you think that adding another layer, particularly a stateless layer (HTTP), with all it’s overhead, can possibly be more efficient than a direct, persistent database connection?

1 Like

You are right It doesn’t make sense, I was looking for a quick way, which worked by the way but it’s counterintuitive. A direct database connection makes more sense. I just didn’t want to struggle with inspect_db. Let me try it.