1. ==============================================
    
  2. How to integrate 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 <DATABASE-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. By default, :djadmin:`inspectdb` creates unmanaged models. That is,
    
  53. ``managed = False`` in the model's ``Meta`` class tells Django not to manage
    
  54. each table's creation, modification, and deletion::
    
  55. 
    
  56.     class Person(models.Model):
    
  57.         id = models.IntegerField(primary_key=True)
    
  58.         first_name = models.CharField(max_length=70)
    
  59.         class Meta:
    
  60.            managed = False
    
  61.            db_table = 'CENSUS_PERSONS'
    
  62. 
    
  63. If you do want to allow Django to manage the table's lifecycle, you'll need to
    
  64. change the :attr:`~django.db.models.Options.managed` option above to ``True``
    
  65. (or remove it because ``True`` is its default value).
    
  66. 
    
  67. Install the core Django tables
    
  68. ==============================
    
  69. 
    
  70. Next, run the :djadmin:`migrate` command to install any extra needed database
    
  71. records such as admin permissions and content types::
    
  72. 
    
  73.     $ python manage.py migrate
    
  74. 
    
  75. Test and tweak
    
  76. ==============
    
  77. 
    
  78. Those are the basic steps -- from here you'll want to tweak the models Django
    
  79. generated until they work the way you'd like. Try accessing your data via the
    
  80. Django database API, and try editing objects via Django's admin site, and edit
    
  81. the models file accordingly.