2021-07-13 15:25:37 +08:00
|
|
|
==============================================
|
|
|
|
How to integrate Django with a legacy database
|
|
|
|
==============================================
|
2008-08-24 06:25:40 +08:00
|
|
|
|
|
|
|
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.
|
|
|
|
|
2014-03-25 22:17:23 +08: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 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
|
|
|
|
2014-03-25 22:17:23 +08: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 22:54:55 +08:00
|
|
|
(or remove it because ``True`` is its default value).
|
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.
|