| 1 |
================================== |
|---|
| 2 |
Integrating with a legacy database |
|---|
| 3 |
================================== |
|---|
| 4 |
|
|---|
| 5 |
While Django is best suited for developing new applications, it's quite |
|---|
| 6 |
possible to integrate it into legacy databases. Django includes a couple of |
|---|
| 7 |
utilities to automate as much of this process as possible. |
|---|
| 8 |
|
|---|
| 9 |
This document assumes you know the Django basics, as covered in the |
|---|
| 10 |
`official tutorial`_. |
|---|
| 11 |
|
|---|
| 12 |
.. _official tutorial: http://www.djangoproject.com/documentation/tutorial1/ |
|---|
| 13 |
|
|---|
| 14 |
Give Django your database parameters |
|---|
| 15 |
==================================== |
|---|
| 16 |
|
|---|
| 17 |
You'll need to tell Django what your database connection parameters are, and |
|---|
| 18 |
what the name of the database is. Do that by editing these settings in your |
|---|
| 19 |
`settings file`_: |
|---|
| 20 |
|
|---|
| 21 |
* `DATABASE_ENGINE`_ |
|---|
| 22 |
* `DATABASE_USER`_ |
|---|
| 23 |
* `DATABASE_PASSWORD`_ |
|---|
| 24 |
* `DATABASE_NAME`_ |
|---|
| 25 |
* `DATABASE_HOST`_ |
|---|
| 26 |
* `DATABASE_PORT`_ |
|---|
| 27 |
|
|---|
| 28 |
.. _settings file: http://www.djangoproject.com/documentation/settings/ |
|---|
| 29 |
.. _DATABASE_ENGINE: http://www.djangoproject.com/documentation/settings/#database-engine |
|---|
| 30 |
.. _DATABASE_USER: http://www.djangoproject.com/documentation/settings/#database-user |
|---|
| 31 |
.. _DATABASE_PASSWORD: http://www.djangoproject.com/documentation/settings/#database-password |
|---|
| 32 |
.. _DATABASE_NAME: http://www.djangoproject.com/documentation/settings/#database-name |
|---|
| 33 |
.. _DATABASE_HOST: http://www.djangoproject.com/documentation/settings/#database-host |
|---|
| 34 |
.. _DATABASE_PORT: http://www.djangoproject.com/documentation/settings/#database-port |
|---|
| 35 |
|
|---|
| 36 |
Auto-generate the models |
|---|
| 37 |
======================== |
|---|
| 38 |
|
|---|
| 39 |
Django comes with a utility that can create models by introspecting an existing |
|---|
| 40 |
database. You can view the output by running this command:: |
|---|
| 41 |
|
|---|
| 42 |
django-admin.py inspectdb [databasename] --settings=path.to.settings |
|---|
| 43 |
|
|---|
| 44 |
...where "[databasename]" is the name of your database. |
|---|
| 45 |
|
|---|
| 46 |
Save this as a file by using standard Unix output redirection:: |
|---|
| 47 |
|
|---|
| 48 |
django-admin.py inspectdb [databasename] --settings=path.to.settings > appname.py |
|---|
| 49 |
|
|---|
| 50 |
This feature is meant as a shortcut, not as definitive model generation. See |
|---|
| 51 |
the `django-admin.py documentation`_ for more information. |
|---|
| 52 |
|
|---|
| 53 |
Once you've cleaned up the model, put the module in the ``models`` directory of |
|---|
| 54 |
your app, and add it to your ``INSTALLED_APPS`` setting. |
|---|
| 55 |
|
|---|
| 56 |
.. _django-admin.py documentation: http://www.djangoproject.com/documentation/django_admin/ |
|---|
| 57 |
|
|---|
| 58 |
Install the core Django tables |
|---|
| 59 |
============================== |
|---|
| 60 |
|
|---|
| 61 |
Next, run the ``django-admin.py init`` command to install Django's core tables |
|---|
| 62 |
in your database:: |
|---|
| 63 |
|
|---|
| 64 |
django-admin.py init --settings=path.to.settings |
|---|
| 65 |
|
|---|
| 66 |
This won't work if your database already contains tables that have any of the |
|---|
| 67 |
following names: |
|---|
| 68 |
|
|---|
| 69 |
* ``sites`` |
|---|
| 70 |
* ``packages`` |
|---|
| 71 |
* ``content_types`` |
|---|
| 72 |
* ``core_sessions`` |
|---|
| 73 |
* ``auth_permissions`` |
|---|
| 74 |
* ``auth_groups`` |
|---|
| 75 |
* ``auth_users`` |
|---|
| 76 |
* ``auth_messages`` |
|---|
| 77 |
* ``auth_groups_permissions`` |
|---|
| 78 |
* ``auth_users_groups`` |
|---|
| 79 |
* ``auth_users_user_permissions`` |
|---|
| 80 |
|
|---|
| 81 |
If that's the case, try renaming one of your tables to resolve naming |
|---|
| 82 |
conflicts. Currently, there's no way of customizing the names of Django's |
|---|
| 83 |
database tables without editing Django's source code itself. |
|---|
| 84 |
|
|---|
| 85 |
Install metadata about your app |
|---|
| 86 |
=============================== |
|---|
| 87 |
|
|---|
| 88 |
Django has a couple of database tables that contain metadata about your apps. |
|---|
| 89 |
You'll need to execute the SQL output by this command:: |
|---|
| 90 |
|
|---|
| 91 |
django-admin.py sqlinitialdata [appname] --settings=path.to.settings |
|---|
| 92 |
|
|---|
| 93 |
See whether it worked |
|---|
| 94 |
===================== |
|---|
| 95 |
|
|---|
| 96 |
That's it. Try accessing your data via the Django database API, and try editing |
|---|
| 97 |
objects via Django's admin site. |
|---|