2008-08-24 06:25:40 +08:00
|
|
|
=========================================
|
|
|
|
Integrating Django 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
|
2010-08-20 03:27:44 +08:00
|
|
|
:doc:`tutorial </intro/tutorial01>`.
|
2008-08-24 06:25:40 +08:00
|
|
|
|
|
|
|
Once you've got Django set up, you'll follow this general process to integrate
|
|
|
|
with an existing database.
|
|
|
|
|
|
|
|
Give Django your database parameters
|
|
|
|
====================================
|
|
|
|
|
|
|
|
You'll need to tell Django what your database connection parameters are, and
|
2009-12-22 23:18:51 +08:00
|
|
|
what the name of the database is. Do that by editing the :setting:`DATABASES`
|
|
|
|
setting and assigning values to the following keys for the ``'default'``
|
|
|
|
connection:
|
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
* :setting:`NAME`
|
2012-12-25 04:37:36 +08:00
|
|
|
* :setting:`ENGINE <DATABASE-ENGINE>`
|
2011-10-14 08:12:01 +08:00
|
|
|
* :setting:`USER`
|
|
|
|
* :setting:`PASSWORD`
|
|
|
|
* :setting:`HOST`
|
|
|
|
* :setting:`PORT`
|
2008-08-24 06:25:40 +08:00
|
|
|
|
|
|
|
Auto-generate the models
|
|
|
|
========================
|
|
|
|
|
|
|
|
.. highlight:: bash
|
|
|
|
|
|
|
|
Django comes with a utility called :djadmin:`inspectdb` that can create models
|
|
|
|
by introspecting an existing database. You can view the output by running this
|
|
|
|
command::
|
|
|
|
|
2013-09-18 22:35:41 +08:00
|
|
|
$ python manage.py inspectdb
|
2008-08-24 06:25:40 +08:00
|
|
|
|
|
|
|
Save this as a file by using standard Unix output redirection::
|
|
|
|
|
2013-09-18 22:35:41 +08:00
|
|
|
$ python manage.py inspectdb > models.py
|
2008-08-24 06:25:40 +08:00
|
|
|
|
|
|
|
This feature is meant as a shortcut, not as definitive model generation. See the
|
|
|
|
:djadmin:`documentation of inspectdb <inspectdb>` for more information.
|
|
|
|
|
|
|
|
Once you've cleaned up your models, name the file ``models.py`` and put it in
|
|
|
|
the Python package that holds your app. Then add the app to your
|
|
|
|
:setting:`INSTALLED_APPS` setting.
|
|
|
|
|
2013-02-03 08:08:45 +08:00
|
|
|
If your plan is that your Django application(s) modify data (i.e. edit, remove
|
|
|
|
records and create new ones) in the existing database tables corresponding to
|
|
|
|
any of the introspected models then one of the manual review and edit steps
|
|
|
|
you need to perform on the resulting ``models.py`` file is to change the
|
|
|
|
Python declaration of each one of these models to specify it is a
|
|
|
|
:attr:`managed <django.db.models.Options.managed>` one. For example, consider
|
|
|
|
this generated model definition:
|
|
|
|
|
2013-09-25 23:39:38 +08:00
|
|
|
.. code-block:: python
|
|
|
|
:emphasize-lines: 5
|
2013-02-03 08:08:45 +08:00
|
|
|
|
2013-09-18 22:35:41 +08:00
|
|
|
class Person(models.Model):
|
|
|
|
id = models.IntegerField(primary_key=True)
|
|
|
|
first_name = models.CharField(max_length=70)
|
|
|
|
class Meta:
|
2013-09-25 23:39:38 +08:00
|
|
|
managed = False
|
2013-09-18 22:35:41 +08:00
|
|
|
db_table = 'CENSUS_PERSONS'
|
2013-02-03 08:08:45 +08:00
|
|
|
|
|
|
|
If you wanted to modify existing data on your ``CENSUS_PERSONS`` SQL table
|
|
|
|
with Django you'd need to change the ``managed`` option highlighted above to
|
|
|
|
``True`` (or simply remove it to let it because ``True`` is its default value).
|
|
|
|
|
2013-02-20 07:03:33 +08:00
|
|
|
This serves as an explicit opt-in to give your nascent Django project write
|
2013-02-03 08:08:45 +08:00
|
|
|
access to your precious data on a model by model basis.
|
|
|
|
|
|
|
|
.. versionchanged:: 1.6
|
|
|
|
|
2013-03-25 13:53:48 +08:00
|
|
|
The behavior by which introspected models are created as unmanaged ones is new
|
|
|
|
in Django 1.6.
|
2013-02-03 08:08:45 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
Install the core Django tables
|
|
|
|
==============================
|
|
|
|
|
2013-07-25 23:19:36 +08:00
|
|
|
Next, run the :djadmin:`migrate` command to install any extra needed database
|
2008-08-24 06:25:40 +08:00
|
|
|
records such as admin permissions and content types::
|
|
|
|
|
2013-09-18 22:35:41 +08:00
|
|
|
$ python manage.py migrate
|
2008-08-24 06:25:40 +08:00
|
|
|
|
|
|
|
Test and tweak
|
|
|
|
==============
|
|
|
|
|
|
|
|
Those are the basic steps -- from here you'll want to tweak the models Django
|
|
|
|
generated until they work the way you'd like. Try accessing your data via the
|
|
|
|
Django database API, and try editing objects via Django's admin site, and edit
|
|
|
|
the models file accordingly.
|