4.2.txt 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  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. * ...
  196. Security
  197. ~~~~~~~~
  198. * ...
  199. Serialization
  200. ~~~~~~~~~~~~~
  201. * ...
  202. Signals
  203. ~~~~~~~
  204. * ...
  205. Templates
  206. ~~~~~~~~~
  207. * ...
  208. Tests
  209. ~~~~~
  210. * The :option:`test --debug-sql` option now formats SQL queries with
  211. ``sqlparse``.
  212. * The :class:`~django.test.RequestFactory`,
  213. :class:`~django.test.AsyncRequestFactory`, :class:`~django.test.Client`, and
  214. :class:`~django.test.AsyncClient` classes now support the ``headers``
  215. parameter, which accepts a dictionary of header names and values. This allows
  216. a more natural syntax for declaring headers.
  217. .. code-block:: python
  218. # Before:
  219. self.client.get("/home/", HTTP_ACCEPT_LANGUAGE="fr")
  220. await self.async_client.get("/home/", ACCEPT_LANGUAGE="fr")
  221. # After:
  222. self.client.get("/home/", headers={"accept-language": "fr"})
  223. await self.async_client.get("/home/", headers={"accept-language": "fr"})
  224. URLs
  225. ~~~~
  226. * ...
  227. Utilities
  228. ~~~~~~~~~
  229. * The new ``encoder`` parameter for :meth:`django.utils.html.json_script`
  230. function allows customizing a JSON encoder class.
  231. * The private internal vendored copy of ``urllib.parse.urlsplit()`` now strips
  232. ``'\r'``, ``'\n'``, and ``'\t'`` (see :cve:`2022-0391` and :bpo:`43882`).
  233. This is to protect projects that may be incorrectly using the internal
  234. ``url_has_allowed_host_and_scheme()`` function, instead of using one of the
  235. documented functions for handling URL redirects. The Django functions were
  236. not affected.
  237. * The new :func:`django.utils.http.content_disposition_header` function returns
  238. a ``Content-Disposition`` HTTP header value as specified by :rfc:`6266`.
  239. Validators
  240. ~~~~~~~~~~
  241. * The list of common passwords used by ``CommonPasswordValidator`` is updated
  242. to the most recent version.
  243. .. _backwards-incompatible-4.2:
  244. Backwards incompatible changes in 4.2
  245. =====================================
  246. Database backend API
  247. --------------------
  248. This section describes changes that may be needed in third-party database
  249. backends.
  250. * ``DatabaseFeatures.allows_group_by_pk`` is removed as it only remained to
  251. accommodate a MySQL extension that has been supplanted by proper functional
  252. dependency detection in MySQL 5.7.15. Note that
  253. ``DatabaseFeatures.allows_group_by_selected_pks`` is still supported and
  254. should be enabled if your backend supports functional dependency detection in
  255. ``GROUP BY`` clauses as specified by the ``SQL:1999`` standard.
  256. Dropped support for MariaDB 10.3
  257. --------------------------------
  258. Upstream support for MariaDB 10.3 ends in May 2023. Django 4.2 supports MariaDB
  259. 10.4 and higher.
  260. Dropped support for MySQL 5.7
  261. -----------------------------
  262. Upstream support for MySQL 5.7 ends in October 2023. Django 4.2 supports MySQL
  263. 8 and higher.
  264. Dropped support for PostgreSQL 11
  265. ---------------------------------
  266. Upstream support for PostgreSQL 11 ends in November 2023. Django 4.2 supports
  267. PostgreSQL 12 and higher.
  268. Setting ``update_fields`` in ``Model.save()`` may now be required
  269. -----------------------------------------------------------------
  270. In order to avoid updating unnecessary columns,
  271. :meth:`.QuerySet.update_or_create` now passes ``update_fields`` to the
  272. :meth:`Model.save() <django.db.models.Model.save>` calls. As a consequence, any
  273. fields modified in the custom ``save()`` methods should be added to the
  274. ``update_fields`` keyword argument before calling ``super()``. See
  275. :ref:`overriding-model-methods` for more details.
  276. Miscellaneous
  277. -------------
  278. * The undocumented ``SimpleTemplateResponse.rendering_attrs`` and
  279. ``TemplateResponse.rendering_attrs`` are renamed to ``non_picklable_attrs``.
  280. * The undocumented ``django.http.multipartparser.parse_header()`` function is
  281. removed. Use ``django.utils.http.parse_header_parameters()`` instead.
  282. * :ttag:`{% blocktranslate asvar … %}<blocktranslate>` result is now marked as
  283. safe for (HTML) output purposes.
  284. * The ``autofocus`` HTML attribute in the admin search box is removed as it can
  285. be confusing for screen readers.
  286. * The :option:`makemigrations --check` option no longer creates missing
  287. migration files.
  288. * The ``alias`` argument for :meth:`.Expression.get_group_by_cols` is removed.
  289. * The minimum supported version of ``sqlparse`` is increased from 0.2.2 to
  290. 0.2.3.
  291. * The undocumented ``negated`` parameter of the
  292. :class:`~django.db.models.Exists` expression is removed.
  293. * The ``is_summary`` argument of the undocumented ``Query.add_annotation()``
  294. method is removed.
  295. * The minimum supported version of SQLite is increased from 3.9.0 to 3.21.0.
  296. * :djadmin:`inspectdb` now uses ``display_size`` from
  297. ``DatabaseIntrospection.get_table_description()`` rather than
  298. ``internal_size`` for ``CharField``.
  299. .. _deprecated-features-4.2:
  300. Features deprecated in 4.2
  301. ==========================
  302. ``index_together`` option is deprecated in favor of ``indexes``
  303. ---------------------------------------------------------------
  304. The :attr:`Meta.index_together <django.db.models.Options.index_together>`
  305. option is deprecated in favor of the :attr:`~django.db.models.Options.indexes`
  306. option.
  307. Migrating existing ``index_together`` should be handled as a migration. For
  308. example::
  309. class Author(models.Model):
  310. rank = models.IntegerField()
  311. name = models.CharField(max_length=30)
  312. class Meta:
  313. index_together = [["rank", "name"]]
  314. Should become::
  315. class Author(models.Model):
  316. rank = models.IntegerField()
  317. name = models.CharField(max_length=30)
  318. class Meta:
  319. indexes = [models.Index(fields=["rank", "name"])]
  320. Running the :djadmin:`makemigrations` command will generate a migration
  321. containing a :class:`~django.db.migrations.operations.RenameIndex` operation
  322. which will rename the existing index.
  323. The ``AlterIndexTogether`` migration operation is now officially supported only
  324. for pre-Django 4.2 migration files. For backward compatibility reasons, it's
  325. still part of the public API, and there's no plan to deprecate or remove it,
  326. but it should not be used for new migrations. Use
  327. :class:`~django.db.migrations.operations.AddIndex` and
  328. :class:`~django.db.migrations.operations.RemoveIndex` operations instead.
  329. Passing encoded JSON string literals to ``JSONField`` is deprecated
  330. -------------------------------------------------------------------
  331. ``JSONField`` and its associated lookups and aggregates use to allow passing
  332. JSON encoded string literals which caused ambiguity on whether string literals
  333. were already encoded from database backend's perspective.
  334. During the deprecation period string literals will be attempted to be JSON
  335. decoded and a warning will be emitted on success that points at passing
  336. non-encoded forms instead.
  337. Code that use to pass JSON encoded string literals::
  338. Document.objects.bulk_create(
  339. Document(data=Value("null")),
  340. Document(data=Value("[]")),
  341. Document(data=Value('"foo-bar"')),
  342. )
  343. Document.objects.annotate(
  344. JSONBAgg("field", default=Value('[]')),
  345. )
  346. Should become::
  347. Document.objects.bulk_create(
  348. Document(data=Value(None, JSONField())),
  349. Document(data=[]),
  350. Document(data="foo-bar"),
  351. )
  352. Document.objects.annotate(
  353. JSONBAgg("field", default=[]),
  354. )
  355. From Django 5.1+ string literals will be implicitly interpreted as JSON string
  356. literals.
  357. Miscellaneous
  358. -------------
  359. * The ``BaseUserManager.make_random_password()`` method is deprecated. See
  360. `recipes and best practices
  361. <https://docs.python.org/3/library/secrets.html#recipes-and-best-practices>`_
  362. for using Python's :py:mod:`secrets` module to generate passwords.
  363. * The ``length_is`` template filter is deprecated in favor of :tfilter:`length`
  364. and the ``==`` operator within an :ttag:`{% if %}<if>` tag. For example
  365. .. code-block:: html+django
  366. {% if value|length == 4 %}…{% endif %}
  367. {% if value|length == 4 %}True{% else %}False{% endif %}
  368. instead of:
  369. .. code-block:: html+django
  370. {% if value|length_is:4 %}…{% endif %}
  371. {{ value|length_is:4 }}
  372. * ``django.contrib.auth.hashers.SHA1PasswordHasher``,
  373. ``django.contrib.auth.hashers.UnsaltedSHA1PasswordHasher``, and
  374. ``django.contrib.auth.hashers.UnsaltedMD5PasswordHasher`` are deprecated.
  375. * ``django.contrib.postgres.fields.CICharField`` is deprecated in favor of
  376. ``CharField(db_collation="…")`` with a case-insensitive non-deterministic
  377. collation.
  378. * ``django.contrib.postgres.fields.CIEmailField`` is deprecated in favor of
  379. ``EmailField(db_collation="…")`` with a case-insensitive non-deterministic
  380. collation.
  381. * ``django.contrib.postgres.fields.CITextField`` is deprecated in favor of
  382. ``TextField(db_collation="…")`` with a case-insensitive non-deterministic
  383. collation.
  384. * ``django.contrib.postgres.fields.CIText`` mixin is deprecated.
  385. * The ``map_height`` and ``map_width`` attributes of ``BaseGeometryWidget`` are
  386. deprecated, use CSS to size map widgets instead.
  387. * ``SimpleTestCase.assertFormsetError()`` is deprecated in favor of
  388. ``assertFormSetError()``.
  389. * ``TransactionTestCase.assertQuerysetEqual()`` is deprecated in favor of
  390. ``assertQuerySetEqual()``.
  391. * Passing positional arguments to ``Signer`` and ``TimestampSigner`` is
  392. deprecated in favor of keyword-only arguments.