legacy-databases.txt 2.9 KB

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