legacy-databases.txt 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 <DATABASE-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. If your plan is that your Django application(s) modify data (i.e. edit, remove
  38. records and create new ones) in the existing database tables corresponding to
  39. any of the introspected models then one of the manual review and edit steps
  40. you need to perform on the resulting ``models.py`` file is to change the
  41. Python declaration of each one of these models to specify it is a
  42. :attr:`managed <django.db.models.Options.managed>` one. For example, consider
  43. this generated model definition:
  44. .. parsed-literal::
  45. class Person(models.Model):
  46. id = models.IntegerField(primary_key=True)
  47. first_name = models.ChaField(max_length=70)
  48. class Meta:
  49. **managed = False**
  50. db_table = 'CENSUS_PERSONS'
  51. If you wanted to modify existing data on your ``CENSUS_PERSONS`` SQL table
  52. with Django you'd need to change the ``managed`` option highlighted above to
  53. ``True`` (or simply remove it to let it because ``True`` is its default value).
  54. This servers as an explicit opt-in to give your nascent Django project write
  55. access to your precious data on a model by model basis.
  56. .. versionchanged:: 1.6
  57. The behavior by which introspected models are created as unmanaged ones is new
  58. in Django 1.6.
  59. Install the core Django tables
  60. ==============================
  61. Next, run the :djadmin:`syncdb` command to install any extra needed database
  62. records such as admin permissions and content types::
  63. python manage.py syncdb
  64. Test and tweak
  65. ==============
  66. Those are the basic steps -- from here you'll want to tweak the models Django
  67. generated until they work the way you'd like. Try accessing your data via the
  68. Django database API, and try editing objects via Django's admin site, and edit
  69. the models file accordingly.