|
1 ========================================= |
|
2 Integrating 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` |
|
25 * :setting:`USER` |
|
26 * :setting:`PASSWORD` |
|
27 * :setting:`HOST` |
|
28 * :setting:`PORT` |
|
29 |
|
30 Auto-generate the models |
|
31 ======================== |
|
32 |
|
33 .. highlight:: bash |
|
34 |
|
35 Django comes with a utility called :djadmin:`inspectdb` that can create models |
|
36 by introspecting an existing database. You can view the output by running this |
|
37 command:: |
|
38 |
|
39 python manage.py inspectdb |
|
40 |
|
41 Save this as a file by using standard Unix output redirection:: |
|
42 |
|
43 python manage.py inspectdb > models.py |
|
44 |
|
45 This feature is meant as a shortcut, not as definitive model generation. See the |
|
46 :djadmin:`documentation of inspectdb <inspectdb>` for more information. |
|
47 |
|
48 Once you've cleaned up your models, name the file ``models.py`` and put it in |
|
49 the Python package that holds your app. Then add the app to your |
|
50 :setting:`INSTALLED_APPS` setting. |
|
51 |
|
52 Install the core Django tables |
|
53 ============================== |
|
54 |
|
55 Next, run the :djadmin:`syncdb` command to install any extra needed database |
|
56 records such as admin permissions and content types:: |
|
57 |
|
58 python manage.py syncdb |
|
59 |
|
60 Test and tweak |
|
61 ============== |
|
62 |
|
63 Those are the basic steps -- from here you'll want to tweak the models Django |
|
64 generated until they work the way you'd like. Try accessing your data via the |
|
65 Django database API, and try editing objects via Django's admin site, and edit |
|
66 the models file accordingly. |