2

I am attempting to build a django based web page (I know python and want to develop a couple web based applicaitons). The goal of this page is to show data from a few different databases that I only have read access to. Basically, it is dashboard type view for a couple key things.

I've build a Sqlite database to hold the Django settings for the application. How do I branch out from here though? I'd like to query an Oracle database and a MySQL database?

I can query them individually using something like this in an appropriate view:

def test(request):
    from django.db import load_backend
    myBackend = load_backend('django.db.backends.oracle') # or 'mysql', 'sqlite3', 'oracle'
    myConnection = myBackend.DatabaseWrapper({
        'HOST': 'myhost',
        'NAME': 'mysid',
        'OPTIONS': {},
        'PASSWORD': "mypass",
        'PORT': "1521",
        'USER': "myuser", 
        'TIME_ZONE': "America/Chicago"})

    # Now we can do all the standard raw sql stuff with myConnection.
    myCursor = myConnection.cursor()
    myCursor.execute("SELECT COUNT(1) FROM schema.table;")
    return HttpResponse("%s." % myCursor.fetchone())

In the template I can display this count. However, this seems incorrect, based on how I can query the Sqlite database.

I'd like to use a data model. Is this the appropriate thing to use when I am just reading data? The account doesn't have write access to the oracle/mysql tables.

Is there a way to automatically generate these models for the existing tables? Some of these tables are rather large in terms of number of columns and, if possible, I'd prefer to automate the create to prevent mistakes on my part.

1 Answer 1

3

For multiple database support see the Django documentation. Long and short, you add the connection info for each database to your DATABASES setting and then setup a router to determine which apps/models each database handles.

For generating models from existing database tables you can use the inspectdb management command. However, Django doesn't explicitly list support for Oracle databases with this command, so you might be on your own there.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.