upgrade-version.txt 5.0 KB

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