legacy-databases.txt 2.2 KB

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