| 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: ../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_NAME` |
|---|
| 22 |
* `DATABASE_ENGINE`_ |
|---|
| 23 |
* `DATABASE_USER`_ |
|---|
| 24 |
* `DATABASE_PASSWORD`_ |
|---|
| 25 |
* `DATABASE_HOST`_ |
|---|
| 26 |
* `DATABASE_PORT`_ |
|---|
| 27 |
|
|---|
| 28 |
.. _settings file: ../settings/ |
|---|
| 29 |
.. _DATABASE_NAME: ../settings/#database-name |
|---|
| 30 |
.. _DATABASE_ENGINE: ../settings/#database-engine |
|---|
| 31 |
.. _DATABASE_USER: ../settings/#database-user |
|---|
| 32 |
.. _DATABASE_PASSWORD: ../settings/#database-password |
|---|
| 33 |
.. _DATABASE_HOST: ../settings/#database-host |
|---|
| 34 |
.. _DATABASE_PORT: ../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 --settings=path.to.settings |
|---|
| 43 |
|
|---|
| 44 |
Save this as a file by using standard Unix output redirection:: |
|---|
| 45 |
|
|---|
| 46 |
django-admin.py inspectdb --settings=path.to.settings > models.py |
|---|
| 47 |
|
|---|
| 48 |
This feature is meant as a shortcut, not as definitive model generation. See |
|---|
| 49 |
the `django-admin.py documentation`_ for more information. |
|---|
| 50 |
|
|---|
| 51 |
Once you've cleaned up your models, name the file ``models.py`` and put it in |
|---|
| 52 |
the Python package that holds your app. Then add the app to your |
|---|
| 53 |
``INSTALLED_APPS`` setting. |
|---|
| 54 |
|
|---|
| 55 |
.. _django-admin.py documentation: ../django_admin/ |
|---|
| 56 |
|
|---|
| 57 |
Install the core Django tables |
|---|
| 58 |
============================== |
|---|
| 59 |
|
|---|
| 60 |
Next, run the ``manage.py syncdb`` command to install any extra needed database |
|---|
| 61 |
records such as admin permissions and content types:: |
|---|
| 62 |
|
|---|
| 63 |
django-admin.py init --settings=path.to.settings |
|---|
| 64 |
|
|---|
| 65 |
See whether it worked |
|---|
| 66 |
===================== |
|---|
| 67 |
|
|---|
| 68 |
That's it. Try accessing your data via the Django database API, and try editing |
|---|
| 69 |
objects via Django's admin site. |
|---|