#

## Django documentation

# Integrating with a legacy database

While Django is best suited for developing new applications, it's quite possible to integrate it into legacy databases. Django includes a couple of utilities to automate as much of this process as possible.

This document assumes you know the Django basics, as covered in the official tutorial.

## Give Django your database parameters

You'll need to tell Django what your database connection parameters are, and what the name of the database is. Do that by editing these settings in your settings file:

> - DATABASE_ENGINE
> - DATABASE_USER
> - DATABASE_PASSWORD
> - DATABASE_NAME
> - DATABASE_HOST
> - DATABASE_PORT

## Auto-generate the models

Django comes with a utility that can create models by introspecting an existing database. You can view the output by running this command:

    django-admin.py inspectdb [databasename] --settings=path.to.settings

...where "[databasename]" is the name of your database.

Save this as a file by using standard Unix output redirection:

    django-admin.py inspectdb [databasename] --settings=path.to.settings > appname.py

This feature is meant as a shortcut, not as definitive model generation. See the django-admin.py documentation for more information.

Once you've cleaned up the model, put the module in the `models` directory of your app, and add it to your `INSTALLED_APPS` setting.

## Install the core Django tables

Next, run the `django-admin.py init` command to install Django's core tables in your database:

    django-admin.py init --settings=path.to.settings

This won't work if your database already contains tables that have any of the following names:

> - `sites`
> - `packages`
> - `content_types`
> - `core_sessions`
> - `auth_permissions`
> - `auth_groups`
> - `auth_users`
> - `auth_messages`
> - `auth_groups_permissions`
> - `auth_users_groups`
> - `auth_users_user_permissions`

If that's the case, try renaming one of your tables to resolve naming conflicts. Currently, there's no way of customizing the names of Django's database tables without editing Django's source code itself.

## Install metadata about your app

Django has a couple of database tables that contain metadata about your apps. You'll need to execute the SQL output by this command:

    django-admin.py sqlinitialdata [appname] --settings=path.to.settings

## See whether it worked

That's it. Try accessing your data via the Django database API, and try editing objects via Django's admin site.

## Comments

### Sam Newman August 18, 2005 at 2:20 a.m.

Of course this would be better titled "How to use Django with a legacy database where the schema matches the models I want". Previous apllication development can leave you with horrible schemas that no-one in their right mind would want to manipulate as a model. One way to resolve this is to wrap the cruddy schema in views and generate the python model from those.

### Tim Keating August 19, 2005 at 8:10 p.m.

Yeah for the port I'm working on, I just created the models I wanted in the new schema, then wrote insert queries that copied the data over. Pretty painless, actually.

### Chris Conver August 22, 2005 at 12:32 p.m.

Please post how to integrate with FirebirdSQL database (or perhaps even JDBC)? Thank you.

### Radek August 26, 2005 at 8:08 a.m.

"try renaming one of your tables to resolve naming conflicts"

Could Django use table prefix, which is set in the settings file?

This is quite common on shared hostings, where you have just one DB available, but want to have it used by several applications.

## Post a comment

Note: Please only use the comments for questions/critcisms/suggestions on the docs; if you experience errors please file a ticket, ask in the IRC channel, or post to the django-users list. Comments will be periodically reviewed, integrated into the documentation proper, and removed.

Your name:

Comment: