Plain Text | 66 lines | 47 code | 19 blank | 0 comment | 0 complexity | 8d67d5a5eb89369c89a9e1aacf675668 MD5 | raw file
Possible License(s): BSD-3-Clause
1========================================= 2Integrating Django with a legacy database 3========================================= 4 5While Django is best suited for developing new applications, it's quite 6possible to integrate it into legacy databases. Django includes a couple of 7utilities to automate as much of this process as possible. 8 9This document assumes you know the Django basics, as covered in the 10:doc:`tutorial </intro/tutorial01>`. 11 12Once you've got Django set up, you'll follow this general process to integrate 13with an existing database. 14 15Give Django your database parameters 16==================================== 17 18You'll need to tell Django what your database connection parameters are, and 19what the name of the database is. Do that by editing the :setting:`DATABASES` 20setting and assigning values to the following keys for the ``'default'`` 21connection: 22 23 * :setting:`NAME` 24 * :setting:`ENGINE` 25 * :setting:`USER` 26 * :setting:`PASSWORD` 27 * :setting:`HOST` 28 * :setting:`PORT` 29 30Auto-generate the models 31======================== 32 33.. highlight:: bash 34 35Django comes with a utility called :djadmin:`inspectdb` that can create models 36by introspecting an existing database. You can view the output by running this 37command:: 38 39 python manage.py inspectdb 40 41Save this as a file by using standard Unix output redirection:: 42 43 python manage.py inspectdb > models.py 44 45This feature is meant as a shortcut, not as definitive model generation. See the 46:djadmin:`documentation of inspectdb <inspectdb>` for more information. 47 48Once you've cleaned up your models, name the file ``models.py`` and put it in 49the Python package that holds your app. Then add the app to your 50:setting:`INSTALLED_APPS` setting. 51 52Install the core Django tables 53============================== 54 55Next, run the :djadmin:`syncdb` command to install any extra needed database 56records such as admin permissions and content types:: 57 58 python manage.py syncdb 59 60Test and tweak 61============== 62 63Those are the basic steps -- from here you'll want to tweak the models Django 64generated until they work the way you'd like. Try accessing your data via the 65Django database API, and try editing objects via Django's admin site, and edit 66the models file accordingly.