2008-08-23 22:25:40 +00: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-19 19:27:44 +00:00
|
|
|
:doc:`tutorial </intro/tutorial01>`.
|
2008-08-23 22:25:40 +00: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 15:18:51 +00: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 00:12:01 +00:00
|
|
|
* :setting:`NAME`
|
2012-12-24 20:37:36 +00:00
|
|
|
* :setting:`ENGINE <DATABASE-ENGINE>`
|
2011-10-14 00:12:01 +00:00
|
|
|
* :setting:`USER`
|
|
|
|
* :setting:`PASSWORD`
|
|
|
|
* :setting:`HOST`
|
|
|
|
* :setting:`PORT`
|
2008-08-23 22:25:40 +00: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 14:35:41 +00:00
|
|
|
$ python manage.py inspectdb
|
2008-08-23 22:25:40 +00:00
|
|
|
|
|
|
|
Save this as a file by using standard Unix output redirection::
|
|
|
|
|
2013-09-18 14:35:41 +00:00
|
|
|
$ python manage.py inspectdb > models.py
|
2008-08-23 22:25:40 +00: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.
|
|
|
|
|
2014-03-25 14:17:23 +00:00
|
|
|
By default, :djadmin:`inspectdb` creates unmanaged models. That is,
|
|
|
|
``managed = False`` in the model's ``Meta`` class tells Django not to manage
|
|
|
|
each table's creation, modification, and deletion::
|
2013-02-03 00:08:45 +00:00
|
|
|
|
2013-09-18 14:35:41 +00:00
|
|
|
class Person(models.Model):
|
|
|
|
id = models.IntegerField(primary_key=True)
|
|
|
|
first_name = models.CharField(max_length=70)
|
|
|
|
class Meta:
|
2013-09-25 15:39:38 +00:00
|
|
|
managed = False
|
2013-09-18 14:35:41 +00:00
|
|
|
db_table = 'CENSUS_PERSONS'
|
2013-02-03 00:08:45 +00:00
|
|
|
|
2014-03-25 14:17:23 +00:00
|
|
|
If you do want to allow Django to manage the table's lifecycle, you'll need to
|
|
|
|
change the :attr:`~django.db.models.Options.managed` option above to ``True``
|
2019-06-17 14:54:55 +00:00
|
|
|
(or remove it because ``True`` is its default value).
|
2013-02-03 00:08:45 +00:00
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
Install the core Django tables
|
|
|
|
==============================
|
|
|
|
|
2013-07-25 15:19:36 +00:00
|
|
|
Next, run the :djadmin:`migrate` command to install any extra needed database
|
2008-08-23 22:25:40 +00:00
|
|
|
records such as admin permissions and content types::
|
|
|
|
|
2013-09-18 14:35:41 +00:00
|
|
|
$ python manage.py migrate
|
2008-08-23 22:25:40 +00: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.
|