Debian Codegraph @

docs/howto/legacy-databases.txt

raw
1 ==============================================
2 How to integrate Django 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 :doc:`tutorial </intro/tutorial01>`.
11
12 Once you've got Django set up, you'll follow this general process to integrate
13 with an existing database.
14
15 Give Django your database parameters
16 ====================================
17
18 You'll need to tell Django what your database connection parameters are, and
19 what the name of the database is. Do that by editing the :setting:`DATABASES`
20 setting and assigning values to the following keys for the ``'default'``
21 connection:
22
23 * :setting:`NAME`
24 * :setting:`ENGINE <DATABASE-ENGINE>`
25 * :setting:`USER`
26 * :setting:`PASSWORD`
27 * :setting:`HOST`
28 * :setting:`PORT`
29
30 Auto-generate the models
31 ========================
32
33 Django comes with a utility called :djadmin:`inspectdb` that can create models
34 by introspecting an existing database. You can view the output by running this
35 command:
36
37 .. code-block:: shell
38
39 $ python manage.py inspectdb
40
41 Save this as a file by using standard Unix output redirection:
42
43 .. code-block:: shell
44
45 $ python manage.py inspectdb > models.py
46
47 This feature is meant as a shortcut, not as definitive model generation. See the
48 :djadmin:`documentation of inspectdb <inspectdb>` for more information.
49
50 Once you've cleaned up your models, name the file ``models.py`` and put it in
51 the Python package that holds your app. Then add the app to your
52 :setting:`INSTALLED_APPS` setting.
53
54 By default, :djadmin:`inspectdb` creates unmanaged models. That is,
55 ``managed = False`` in the model's ``Meta`` class tells Django not to manage
56 each table's creation, modification, and deletion::
57
58 class Person(models.Model):
59 id = models.IntegerField(primary_key=True)
60 first_name = models.CharField(max_length=70)
61
62 class Meta:
63 managed = False
64 db_table = "CENSUS_PERSONS"
65
66 If you do want to allow Django to manage the table's lifecycle, you'll need to
67 change the :attr:`~django.db.models.Options.managed` option above to ``True``
68 (or remove it because ``True`` is its default value).
69
70 Install the core Django tables
71 ==============================
72
73 Next, run the :djadmin:`migrate` command to install any extra needed database
74 records such as admin permissions and content types:
75
76 .. code-block:: shell
77
78 $ python manage.py migrate
79
80 Test and tweak
81 ==============
82
83 Those are the basic steps -- from here you'll want to tweak the models Django
84 generated until they work the way you'd like. Try accessing your data via the
85 Django database API, and try editing objects via Django's admin site, and edit
86 the models file accordingly.