1. ========================================
    
  2. How to upgrade Django to a newer version
    
  3. ========================================
    
  4. 
    
  5. While it can be a complex process at times, upgrading to the latest Django
    
  6. version has several benefits:
    
  7. 
    
  8. * New features and improvements are added.
    
  9. * Bugs are fixed.
    
  10. * Older version of Django will eventually no longer receive security updates.
    
  11.   (see :ref:`supported-versions-policy`).
    
  12. * Upgrading as each new Django release is available makes future upgrades less
    
  13.   painful by keeping your code base up to date.
    
  14. 
    
  15. Here are some things to consider to help make your upgrade process as smooth as
    
  16. possible.
    
  17. 
    
  18. Required Reading
    
  19. ================
    
  20. 
    
  21. If it's your first time doing an upgrade, it is useful to read the :doc:`guide
    
  22. on the different release processes </internals/release-process>`.
    
  23. 
    
  24. Afterward, you should familiarize yourself with the changes that were made in
    
  25. the new Django version(s):
    
  26. 
    
  27. * Read the :doc:`release notes </releases/index>` for each 'final' release from
    
  28.   the one after your current Django version, up to and including the version to
    
  29.   which you plan to upgrade.
    
  30. * Look at the :doc:`deprecation timeline</internals/deprecation>` for the
    
  31.   relevant versions.
    
  32. 
    
  33. Pay particular attention to backwards incompatible changes to get a clear idea
    
  34. of what will be needed for a successful upgrade.
    
  35. 
    
  36. If you're upgrading through more than one feature version (e.g. 2.0 to 2.2),
    
  37. it's usually easier to upgrade through each feature release incrementally
    
  38. (2.0 to 2.1 to 2.2) rather than to make all the changes for each feature
    
  39. release at once. For each feature release, use the latest patch release (e.g.
    
  40. for 2.1, use 2.1.15).
    
  41. 
    
  42. The same incremental upgrade approach is recommended when upgrading from one
    
  43. LTS to the next.
    
  44. 
    
  45. Dependencies
    
  46. ============
    
  47. 
    
  48. In most cases it will be necessary to upgrade to the latest version of your
    
  49. Django-related dependencies as well. If the Django version was recently
    
  50. released or if some of your dependencies are not well-maintained, some of your
    
  51. dependencies may not yet support the new Django version. In these cases you may
    
  52. have to wait until new versions of your dependencies are released.
    
  53. 
    
  54. Resolving deprecation warnings
    
  55. ==============================
    
  56. 
    
  57. Before upgrading, it's a good idea to resolve any deprecation warnings raised
    
  58. by your project while using your current version of Django. Fixing these
    
  59. warnings before upgrading ensures that you're informed about areas of the code
    
  60. that need altering.
    
  61. 
    
  62. In Python, deprecation warnings are silenced by default. You must turn them on
    
  63. using the ``-Wa`` Python command line option or the :envvar:`PYTHONWARNINGS`
    
  64. environment variable. For example, to show warnings while running tests:
    
  65. 
    
  66. .. console::
    
  67. 
    
  68.     $ python -Wa manage.py test
    
  69. 
    
  70. If you're not using the Django test runner, you may need to also ensure that
    
  71. any console output is not captured which would hide deprecation warnings. For
    
  72. example, if you use `pytest <https://docs.pytest.org/>`__:
    
  73. 
    
  74. .. code-block:: console
    
  75. 
    
  76.     $ PYTHONWARNINGS=always pytest tests --capture=no
    
  77. 
    
  78. Resolve any deprecation warnings with your current version of Django before
    
  79. continuing the upgrade process.
    
  80. 
    
  81. Third party applications might use deprecated APIs in order to support multiple
    
  82. versions of Django, so deprecation warnings in packages you've installed don't
    
  83. necessarily indicate a problem. If a package doesn't support the latest version
    
  84. of Django, consider raising an issue or sending a pull request for it.
    
  85. 
    
  86. Installation
    
  87. ============
    
  88. 
    
  89. Once you're ready, it is time to :doc:`install the new Django version
    
  90. </topics/install>`. If you are using a :mod:`virtual environment <venv>` and it
    
  91. is a major upgrade, you might want to set up a new environment with all the
    
  92. dependencies first.
    
  93. 
    
  94. If you installed Django with pip_, you can use the ``--upgrade`` or ``-U`` flag:
    
  95. 
    
  96. .. console::
    
  97. 
    
  98.    $ python -m pip install -U Django
    
  99. 
    
  100. .. _pip: https://pip.pypa.io/
    
  101. 
    
  102. Testing
    
  103. =======
    
  104. 
    
  105. When the new environment is set up, :doc:`run the full test suite
    
  106. </topics/testing/overview>` for your application. Again, it's useful to turn
    
  107. on deprecation warnings on so they're shown in the test output (you can also
    
  108. use the flag if you test your app manually using ``manage.py runserver``):
    
  109. 
    
  110. .. console::
    
  111. 
    
  112.     $ python -Wa manage.py test
    
  113. 
    
  114. After you have run the tests, fix any failures. While you have the release
    
  115. notes fresh in your mind, it may also be a good time to take advantage of new
    
  116. features in Django by refactoring your code to eliminate any deprecation
    
  117. warnings.
    
  118. 
    
  119. Deployment
    
  120. ==========
    
  121. 
    
  122. When you are sufficiently confident your app works with the new version of
    
  123. Django, you're ready to go ahead and :doc:`deploy </howto/deployment/index>`
    
  124. your upgraded Django project.
    
  125. 
    
  126. If you are using caching provided by Django, you should consider clearing your
    
  127. cache after upgrading. Otherwise you may run into problems, for example, if you
    
  128. are caching pickled objects as these objects are not guaranteed to be
    
  129. pickle-compatible across Django versions. A past instance of incompatibility
    
  130. was caching pickled :class:`~django.http.HttpResponse` objects, either
    
  131. directly or indirectly via the :func:`~django.views.decorators.cache.cache_page`
    
  132. decorator.