4.2.txt 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  1. ============================================
  2. Django 4.2 release notes - UNDER DEVELOPMENT
  3. ============================================
  4. *Expected April 2023*
  5. Welcome to Django 4.2!
  6. These release notes cover the :ref:`new features <whats-new-4.2>`, as well as
  7. some :ref:`backwards incompatible changes <backwards-incompatible-4.2>` you'll
  8. want to be aware of when upgrading from Django 4.1 or earlier. We've
  9. :ref:`begun the deprecation process for some features
  10. <deprecated-features-4.2>`.
  11. See the :doc:`/howto/upgrade-version` guide if you're updating an existing
  12. project.
  13. Python compatibility
  14. ====================
  15. Django 4.2 supports Python 3.8, 3.9, 3.10, and 3.11. We **highly recommend**
  16. and only officially support the latest release of each series.
  17. .. _whats-new-4.2:
  18. What's new in Django 4.2
  19. ========================
  20. Psycopg 3 support
  21. -----------------
  22. Django now supports `psycopg`_ version 3.1 or higher. To update your code,
  23. install the `psycopg library`_, you don't need to change the
  24. :setting:`ENGINE <DATABASE-ENGINE>` as ``django.db.backends.postgresql``
  25. supports both libraries.
  26. Support for ``psycopg2`` is likely to be deprecated and removed at some point
  27. in the future.
  28. .. _psycopg: https://www.psycopg.org/psycopg3/
  29. .. _psycopg library: https://pypi.org/project/psycopg/
  30. Mitigation for the BREACH attack
  31. --------------------------------
  32. :class:`~django.middleware.gzip.GZipMiddleware` now includes a mitigation for
  33. the BREACH attack. It will add up to 100 random bytes to gzip responses to make
  34. BREACH attacks harder. Read more about the mitigation technique in the `Heal
  35. The Breach (HTB) paper`_.
  36. .. _Heal The Breach (HTB) paper: https://ieeexplore.ieee.org/document/9754554
  37. Minor features
  38. --------------
  39. :mod:`django.contrib.admin`
  40. ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  41. * The light or dark color theme of the admin can now be toggled in the UI, as
  42. well as being set to follow the system setting.
  43. * The admin's font stack now prefers system UI fonts and no longer requires
  44. downloading fonts. Additionally, CSS variables are available to more easily
  45. override the default font families.
  46. * The :source:`admin/delete_confirmation.html
  47. <django/contrib/admin/templates/admin/delete_confirmation.html>` template now
  48. has some additional blocks and scripting hooks to ease customization.
  49. * The chosen options of
  50. :attr:`~django.contrib.admin.ModelAdmin.filter_horizontal` and
  51. :attr:`~django.contrib.admin.ModelAdmin.filter_vertical` widgets are now
  52. filterable.
  53. * The ``admin/base.html`` template now has a new block ``nav-breadcrumbs``
  54. which contains the navigation landmark and the ``breadcrumbs`` block.
  55. * :attr:`.ModelAdmin.list_editable` now uses atomic transactions when making
  56. edits.
  57. :mod:`django.contrib.admindocs`
  58. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  59. * ...
  60. :mod:`django.contrib.auth`
  61. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  62. * The default iteration count for the PBKDF2 password hasher is increased from
  63. 390,000 to 480,000.
  64. * :class:`~django.contrib.auth.forms.UserCreationForm` now saves many-to-many
  65. form fields for a custom user model.
  66. :mod:`django.contrib.contenttypes`
  67. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  68. * ...
  69. :mod:`django.contrib.gis`
  70. ~~~~~~~~~~~~~~~~~~~~~~~~~
  71. * The :doc:`GeoJSON serializer </ref/contrib/gis/serializers>` now outputs the
  72. ``id`` key for serialized features, which defaults to the primary key of
  73. objects.
  74. * The :class:`~django.contrib.gis.gdal.GDALRaster` class now supports
  75. :class:`pathlib.Path`.
  76. * The :class:`~django.contrib.gis.geoip2.GeoIP2` class now supports ``.mmdb``
  77. files downloaded from DB-IP.
  78. * The OpenLayers template widget no longer includes inline CSS (which also
  79. removes the former ``map_css`` block) to better comply with a strict Content
  80. Security Policy.
  81. :mod:`django.contrib.messages`
  82. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  83. * ...
  84. :mod:`django.contrib.postgres`
  85. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  86. * The new :lookup:`trigram_strict_word_similar` lookup, and the
  87. :class:`TrigramStrictWordSimilarity()
  88. <django.contrib.postgres.search.TrigramStrictWordSimilarity>` and
  89. :class:`TrigramStrictWordDistance()
  90. <django.contrib.postgres.search.TrigramStrictWordDistance>` expressions allow
  91. using trigram strict word similarity.
  92. * The :lookup:`arrayfield.overlap` lookup now supports ``QuerySet.values()``
  93. and ``values_list()`` as a right-hand side.
  94. :mod:`django.contrib.redirects`
  95. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  96. * ...
  97. :mod:`django.contrib.sessions`
  98. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  99. * ...
  100. :mod:`django.contrib.sitemaps`
  101. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  102. * The new :meth:`.Sitemap.get_languages_for_item` method allows customizing the
  103. list of languages for which the item is displayed.
  104. :mod:`django.contrib.sites`
  105. ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  106. * ...
  107. :mod:`django.contrib.staticfiles`
  108. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  109. * :class:`~django.contrib.staticfiles.storage.ManifestStaticFilesStorage` now
  110. replaces paths to JavaScript modules in ``import`` and ``export`` statements
  111. with their hashed counterparts.
  112. :mod:`django.contrib.syndication`
  113. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  114. * ...
  115. Cache
  116. ~~~~~
  117. * ...
  118. CSRF
  119. ~~~~
  120. * ...
  121. Decorators
  122. ~~~~~~~~~~
  123. * ...
  124. Email
  125. ~~~~~
  126. * ...
  127. Error Reporting
  128. ~~~~~~~~~~~~~~~
  129. * The debug page now shows :pep:`exception notes <678>` and
  130. :pep:`fine-grained error locations <657>` on Python 3.11+.
  131. File Storage
  132. ~~~~~~~~~~~~
  133. * ...
  134. File Uploads
  135. ~~~~~~~~~~~~
  136. * ...
  137. Forms
  138. ~~~~~
  139. * :class:`~django.forms.ModelForm` now accepts the new ``Meta`` option
  140. ``formfield_callback`` to customize form fields.
  141. * :func:`~django.forms.models.modelform_factory` now respects the
  142. ``formfield_callback`` attribute of the ``form``’s ``Meta``.
  143. * Session cookies are now treated as credentials and therefore hidden and
  144. replaced with stars (``**********``) in error reports.
  145. Generic Views
  146. ~~~~~~~~~~~~~
  147. * ...
  148. Internationalization
  149. ~~~~~~~~~~~~~~~~~~~~
  150. * Added support and translations for the Central Kurdish (Sorani) language.
  151. * The :class:`~django.middleware.locale.LocaleMiddleware` now respects a
  152. language from the request when :func:`~django.conf.urls.i18n.i18n_patterns`
  153. is used with the ``prefix_default_language`` argument set to ``False``.
  154. Logging
  155. ~~~~~~~
  156. * The :ref:`django-db-logger` logger now logs transaction management queries
  157. (``BEGIN``, ``COMMIT``, and ``ROLLBACK``) at the ``DEBUG`` level.
  158. Management Commands
  159. ~~~~~~~~~~~~~~~~~~~
  160. * :djadmin:`makemessages` command now supports locales with private sub-tags
  161. such as ``nl_NL-x-informal``.
  162. * The new :option:`makemigrations --update` option merges model changes into
  163. the latest migration and optimizes the resulting operations.
  164. Migrations
  165. ~~~~~~~~~~
  166. * Migrations now support serialization of ``enum.Flag`` objects.
  167. Models
  168. ~~~~~~
  169. * ``QuerySet`` now extensively supports filtering against
  170. :ref:`window-functions` with the exception of disjunctive filter lookups
  171. against window functions when performing aggregation.
  172. * :meth:`~.QuerySet.prefetch_related` now supports
  173. :class:`~django.db.models.Prefetch` objects with sliced querysets.
  174. * :ref:`Registering lookups <lookup-registration-api>` on
  175. :class:`~django.db.models.Field` instances is now supported.
  176. * The new ``robust`` argument for :func:`~django.db.transaction.on_commit`
  177. allows performing actions that can fail after a database transaction is
  178. successfully committed.
  179. * The new :class:`KT() <django.db.models.fields.json.KT>` expression represents
  180. the text value of a key, index, or path transform of
  181. :class:`~django.db.models.JSONField`.
  182. * :class:`~django.db.models.functions.Now` now supports microsecond precision
  183. on MySQL and millisecond precision on SQLite.
  184. * :class:`F() <django.db.models.F>` expressions that output ``BooleanField``
  185. can now be negated using ``~F()`` (inversion operator).
  186. * ``Model`` now provides asynchronous versions of some methods that use the
  187. database, using an ``a`` prefix: :meth:`~.Model.adelete`,
  188. :meth:`~.Model.arefresh_from_db`, and :meth:`~.Model.asave`.
  189. * Related managers now provide asynchronous versions of methods that change a
  190. set of related objects, using an ``a`` prefix: :meth:`~.RelatedManager.aadd`,
  191. :meth:`~.RelatedManager.aclear`, :meth:`~.RelatedManager.aremove`, and
  192. :meth:`~.RelatedManager.aset`.
  193. Requests and Responses
  194. ~~~~~~~~~~~~~~~~~~~~~~
  195. * :class:`~django.http.StreamingHttpResponse` now supports async iterators
  196. when Django is served via ASGI.
  197. Security
  198. ~~~~~~~~
  199. * ...
  200. Serialization
  201. ~~~~~~~~~~~~~
  202. * ...
  203. Signals
  204. ~~~~~~~
  205. * ...
  206. Templates
  207. ~~~~~~~~~
  208. * ...
  209. Tests
  210. ~~~~~
  211. * The :option:`test --debug-sql` option now formats SQL queries with
  212. ``sqlparse``.
  213. * The :class:`~django.test.RequestFactory`,
  214. :class:`~django.test.AsyncRequestFactory`, :class:`~django.test.Client`, and
  215. :class:`~django.test.AsyncClient` classes now support the ``headers``
  216. parameter, which accepts a dictionary of header names and values. This allows
  217. a more natural syntax for declaring headers.
  218. .. code-block:: python
  219. # Before:
  220. self.client.get("/home/", HTTP_ACCEPT_LANGUAGE="fr")
  221. await self.async_client.get("/home/", ACCEPT_LANGUAGE="fr")
  222. # After:
  223. self.client.get("/home/", headers={"accept-language": "fr"})
  224. await self.async_client.get("/home/", headers={"accept-language": "fr"})
  225. URLs
  226. ~~~~
  227. * ...
  228. Utilities
  229. ~~~~~~~~~
  230. * The new ``encoder`` parameter for :meth:`django.utils.html.json_script`
  231. function allows customizing a JSON encoder class.
  232. * The private internal vendored copy of ``urllib.parse.urlsplit()`` now strips
  233. ``'\r'``, ``'\n'``, and ``'\t'`` (see :cve:`2022-0391` and :bpo:`43882`).
  234. This is to protect projects that may be incorrectly using the internal
  235. ``url_has_allowed_host_and_scheme()`` function, instead of using one of the
  236. documented functions for handling URL redirects. The Django functions were
  237. not affected.
  238. * The new :func:`django.utils.http.content_disposition_header` function returns
  239. a ``Content-Disposition`` HTTP header value as specified by :rfc:`6266`.
  240. Validators
  241. ~~~~~~~~~~
  242. * The list of common passwords used by ``CommonPasswordValidator`` is updated
  243. to the most recent version.
  244. .. _backwards-incompatible-4.2:
  245. Backwards incompatible changes in 4.2
  246. =====================================
  247. Database backend API
  248. --------------------
  249. This section describes changes that may be needed in third-party database
  250. backends.
  251. * ``DatabaseFeatures.allows_group_by_pk`` is removed as it only remained to
  252. accommodate a MySQL extension that has been supplanted by proper functional
  253. dependency detection in MySQL 5.7.15. Note that
  254. ``DatabaseFeatures.allows_group_by_selected_pks`` is still supported and
  255. should be enabled if your backend supports functional dependency detection in
  256. ``GROUP BY`` clauses as specified by the ``SQL:1999`` standard.
  257. Dropped support for MariaDB 10.3
  258. --------------------------------
  259. Upstream support for MariaDB 10.3 ends in May 2023. Django 4.2 supports MariaDB
  260. 10.4 and higher.
  261. Dropped support for MySQL 5.7
  262. -----------------------------
  263. Upstream support for MySQL 5.7 ends in October 2023. Django 4.2 supports MySQL
  264. 8 and higher.
  265. Dropped support for PostgreSQL 11
  266. ---------------------------------
  267. Upstream support for PostgreSQL 11 ends in November 2023. Django 4.2 supports
  268. PostgreSQL 12 and higher.
  269. Setting ``update_fields`` in ``Model.save()`` may now be required
  270. -----------------------------------------------------------------
  271. In order to avoid updating unnecessary columns,
  272. :meth:`.QuerySet.update_or_create` now passes ``update_fields`` to the
  273. :meth:`Model.save() <django.db.models.Model.save>` calls. As a consequence, any
  274. fields modified in the custom ``save()`` methods should be added to the
  275. ``update_fields`` keyword argument before calling ``super()``. See
  276. :ref:`overriding-model-methods` for more details.
  277. Miscellaneous
  278. -------------
  279. * The undocumented ``SimpleTemplateResponse.rendering_attrs`` and
  280. ``TemplateResponse.rendering_attrs`` are renamed to ``non_picklable_attrs``.
  281. * The undocumented ``django.http.multipartparser.parse_header()`` function is
  282. removed. Use ``django.utils.http.parse_header_parameters()`` instead.
  283. * :ttag:`{% blocktranslate asvar … %}<blocktranslate>` result is now marked as
  284. safe for (HTML) output purposes.
  285. * The ``autofocus`` HTML attribute in the admin search box is removed as it can
  286. be confusing for screen readers.
  287. * The :option:`makemigrations --check` option no longer creates missing
  288. migration files.
  289. * The ``alias`` argument for :meth:`.Expression.get_group_by_cols` is removed.
  290. * The minimum supported version of ``sqlparse`` is increased from 0.2.2 to
  291. 0.2.3.
  292. * The undocumented ``negated`` parameter of the
  293. :class:`~django.db.models.Exists` expression is removed.
  294. * The ``is_summary`` argument of the undocumented ``Query.add_annotation()``
  295. method is removed.
  296. * The minimum supported version of SQLite is increased from 3.9.0 to 3.21.0.
  297. * :djadmin:`inspectdb` now uses ``display_size`` from
  298. ``DatabaseIntrospection.get_table_description()`` rather than
  299. ``internal_size`` for ``CharField``.
  300. * The minimum supported version of ``asgiref`` is increased from 3.5.2 to
  301. 3.6.0.
  302. .. _deprecated-features-4.2:
  303. Features deprecated in 4.2
  304. ==========================
  305. ``index_together`` option is deprecated in favor of ``indexes``
  306. ---------------------------------------------------------------
  307. The :attr:`Meta.index_together <django.db.models.Options.index_together>`
  308. option is deprecated in favor of the :attr:`~django.db.models.Options.indexes`
  309. option.
  310. Migrating existing ``index_together`` should be handled as a migration. For
  311. example::
  312. class Author(models.Model):
  313. rank = models.IntegerField()
  314. name = models.CharField(max_length=30)
  315. class Meta:
  316. index_together = [["rank", "name"]]
  317. Should become::
  318. class Author(models.Model):
  319. rank = models.IntegerField()
  320. name = models.CharField(max_length=30)
  321. class Meta:
  322. indexes = [models.Index(fields=["rank", "name"])]
  323. Running the :djadmin:`makemigrations` command will generate a migration
  324. containing a :class:`~django.db.migrations.operations.RenameIndex` operation
  325. which will rename the existing index.
  326. The ``AlterIndexTogether`` migration operation is now officially supported only
  327. for pre-Django 4.2 migration files. For backward compatibility reasons, it's
  328. still part of the public API, and there's no plan to deprecate or remove it,
  329. but it should not be used for new migrations. Use
  330. :class:`~django.db.migrations.operations.AddIndex` and
  331. :class:`~django.db.migrations.operations.RemoveIndex` operations instead.
  332. Passing encoded JSON string literals to ``JSONField`` is deprecated
  333. -------------------------------------------------------------------
  334. ``JSONField`` and its associated lookups and aggregates use to allow passing
  335. JSON encoded string literals which caused ambiguity on whether string literals
  336. were already encoded from database backend's perspective.
  337. During the deprecation period string literals will be attempted to be JSON
  338. decoded and a warning will be emitted on success that points at passing
  339. non-encoded forms instead.
  340. Code that use to pass JSON encoded string literals::
  341. Document.objects.bulk_create(
  342. Document(data=Value("null")),
  343. Document(data=Value("[]")),
  344. Document(data=Value('"foo-bar"')),
  345. )
  346. Document.objects.annotate(
  347. JSONBAgg("field", default=Value('[]')),
  348. )
  349. Should become::
  350. Document.objects.bulk_create(
  351. Document(data=Value(None, JSONField())),
  352. Document(data=[]),
  353. Document(data="foo-bar"),
  354. )
  355. Document.objects.annotate(
  356. JSONBAgg("field", default=[]),
  357. )
  358. From Django 5.1+ string literals will be implicitly interpreted as JSON string
  359. literals.
  360. Miscellaneous
  361. -------------
  362. * The ``BaseUserManager.make_random_password()`` method is deprecated. See
  363. `recipes and best practices
  364. <https://docs.python.org/3/library/secrets.html#recipes-and-best-practices>`_
  365. for using Python's :py:mod:`secrets` module to generate passwords.
  366. * The ``length_is`` template filter is deprecated in favor of :tfilter:`length`
  367. and the ``==`` operator within an :ttag:`{% if %}<if>` tag. For example
  368. .. code-block:: html+django
  369. {% if value|length == 4 %}…{% endif %}
  370. {% if value|length == 4 %}True{% else %}False{% endif %}
  371. instead of:
  372. .. code-block:: html+django
  373. {% if value|length_is:4 %}…{% endif %}
  374. {{ value|length_is:4 }}
  375. * ``django.contrib.auth.hashers.SHA1PasswordHasher``,
  376. ``django.contrib.auth.hashers.UnsaltedSHA1PasswordHasher``, and
  377. ``django.contrib.auth.hashers.UnsaltedMD5PasswordHasher`` are deprecated.
  378. * ``django.contrib.postgres.fields.CICharField`` is deprecated in favor of
  379. ``CharField(db_collation="…")`` with a case-insensitive non-deterministic
  380. collation.
  381. * ``django.contrib.postgres.fields.CIEmailField`` is deprecated in favor of
  382. ``EmailField(db_collation="…")`` with a case-insensitive non-deterministic
  383. collation.
  384. * ``django.contrib.postgres.fields.CITextField`` is deprecated in favor of
  385. ``TextField(db_collation="…")`` with a case-insensitive non-deterministic
  386. collation.
  387. * ``django.contrib.postgres.fields.CIText`` mixin is deprecated.
  388. * The ``map_height`` and ``map_width`` attributes of ``BaseGeometryWidget`` are
  389. deprecated, use CSS to size map widgets instead.
  390. * ``SimpleTestCase.assertFormsetError()`` is deprecated in favor of
  391. ``assertFormSetError()``.
  392. * ``TransactionTestCase.assertQuerysetEqual()`` is deprecated in favor of
  393. ``assertQuerySetEqual()``.
  394. * Passing positional arguments to ``Signer`` and ``TimestampSigner`` is
  395. deprecated in favor of keyword-only arguments.