5.2.txt 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  1. ============================================
  2. Django 5.2 release notes - UNDER DEVELOPMENT
  3. ============================================
  4. *Expected April 2025*
  5. Welcome to Django 5.2!
  6. These release notes cover the :ref:`new features <whats-new-5.2>`, as well as
  7. some :ref:`backwards incompatible changes <backwards-incompatible-5.2>` you
  8. should be aware of when upgrading from Django 5.1 or earlier. We've
  9. :ref:`begun the deprecation process for some features
  10. <deprecated-features-5.2>`.
  11. See the :doc:`/howto/upgrade-version` guide if you're updating an existing
  12. project.
  13. Django 5.2 is designated as a :term:`long-term support release
  14. <Long-term support release>`. It will receive security updates for at least
  15. three years after its release. Support for the previous LTS, Django 4.2, will
  16. end in April 2026.
  17. Python compatibility
  18. ====================
  19. Django 5.2 supports Python 3.10, 3.11, 3.12, and 3.13. We **highly recommend**
  20. and only officially support the latest release of each series.
  21. .. _whats-new-5.2:
  22. What's new in Django 5.2
  23. ========================
  24. Automatic models import in the ``shell``
  25. ----------------------------------------
  26. The :djadmin:`shell` management command now automatically imports models from
  27. all installed apps. You can view further details of the imported objects by
  28. setting the ``--verbosity`` flag to 2 or more:
  29. .. code-block:: pycon
  30. $ python -Wall manage.py shell --verbosity=2
  31. 6 objects imported automatically, including:
  32. from django.contrib.admin.models import LogEntry
  33. from django.contrib.auth.models import Group, Permission, User
  34. from django.contrib.contenttypes.models import ContentType
  35. from django.contrib.sessions.models import Session
  36. This :ref:`behavior can be customized <customizing-shell-auto-imports>` to add
  37. or remove automatic imports.
  38. Composite Primary Keys
  39. ----------------------
  40. The new :class:`django.db.models.CompositePrimaryKey` allows tables to be
  41. created with a primary key consisting of multiple fields.
  42. To use a composite primary key, when defining a model set the ``pk`` attribute
  43. to be a ``CompositePrimaryKey``::
  44. from django.db import models
  45. class Release(models.Model):
  46. pk = models.CompositePrimaryKey("version", "name")
  47. version = models.IntegerField()
  48. name = models.CharField(max_length=20)
  49. See :doc:`/topics/composite-primary-key` for more details.
  50. Simplified override of :class:`~django.forms.BoundField`
  51. --------------------------------------------------------
  52. Prior to version 5.2, overriding :meth:`.Field.get_bound_field()` was the only
  53. option to use a custom :class:`~django.forms.BoundField`. Django now supports
  54. specifying the following attributes to customize form rendering:
  55. * :attr:`.BaseRenderer.bound_field_class` at the project level,
  56. * :attr:`.Form.bound_field_class` at the form level, and
  57. * :attr:`.Field.bound_field_class` at the field level.
  58. For example, to customize the ``BoundField`` of a ``Form`` class::
  59. from django.forms import Form
  60. class CustomBoundField(forms.BoundField):
  61. custom_class = "custom"
  62. def css_classes(self, extra_classes=None):
  63. result = super().css_classes(extra_classes)
  64. if self.custom_class not in result:
  65. result += f" {self.custom_class}"
  66. return result.strip()
  67. class CustomForm(forms.Form):
  68. bound_field_class = CustomBoundField
  69. name = forms.CharField(
  70. label="Your Name",
  71. max_length=100,
  72. required=False,
  73. widget=forms.TextInput(attrs={"class": "name-input-class"}),
  74. )
  75. email = forms.EmailField(label="Your Email")
  76. When rendering a ``CustomForm`` instance, the following HTML is included:
  77. .. code:: html
  78. <div class="custom">
  79. <label for="id_name">Your Name:</label>
  80. <input type="text" name="name" class="name-input-class" maxlength="100" id="id_name">
  81. </div>
  82. <div class="custom">
  83. <label for="id_email">Your Email:</label>
  84. <input type="email" name="email" maxlength="320" required="" id="id_email">
  85. </div>
  86. See :ref:`custom-boundfield` for more details about this feature.
  87. Minor features
  88. --------------
  89. :mod:`django.contrib.admin`
  90. ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  91. * The ``admin/base.html`` template now has a new block
  92. :ref:`extrabody <extrabody>` for adding custom code before the closing
  93. ``</body>`` tag.
  94. * The value of a :class:`~django.db.models.URLField` now renders as a link.
  95. :mod:`django.contrib.admindocs`
  96. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  97. * Links to components in docstrings now supports custom link text, using the
  98. format ``:role:`link text <link>```. See :ref:`documentation helpers
  99. <admindocs-helpers>` for more details.
  100. * The :ref:`model pages <admindocs-model-reference>` are now restricted to
  101. users with the corresponding view or change permissions.
  102. :mod:`django.contrib.auth`
  103. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  104. * The default iteration count for the PBKDF2 password hasher is increased from
  105. 870,000 to 1,000,000.
  106. * The following new asynchronous methods are now provided, using an ``a``
  107. prefix:
  108. * :meth:`.UserManager.acreate_user`
  109. * :meth:`.UserManager.acreate_superuser`
  110. * :meth:`.BaseUserManager.aget_by_natural_key`
  111. * :meth:`.User.aget_user_permissions`
  112. * :meth:`.User.aget_all_permissions`
  113. * :meth:`.User.aget_group_permissions`
  114. * :meth:`.User.ahas_perm`
  115. * :meth:`.User.ahas_perms`
  116. * :meth:`.User.ahas_module_perms`
  117. * :meth:`.User.aget_user_permissions`
  118. * :meth:`.User.aget_group_permissions`
  119. * :meth:`.User.ahas_perm`
  120. * :meth:`.ModelBackend.aauthenticate`
  121. * :meth:`.ModelBackend.aget_user_permissions`
  122. * :meth:`.ModelBackend.aget_group_permissions`
  123. * :meth:`.ModelBackend.aget_all_permissions`
  124. * :meth:`.ModelBackend.ahas_perm`
  125. * :meth:`.ModelBackend.ahas_module_perms`
  126. * :meth:`.RemoteUserBackend.aauthenticate`
  127. * :meth:`.RemoteUserBackend.aconfigure_user`
  128. * Auth backends can now provide async implementations which are used when
  129. calling async auth functions (e.g.
  130. :func:`~.django.contrib.auth.aauthenticate`) to reduce context-switching
  131. which improves performance. See :ref:`adding an async interface
  132. <writing-authentication-backends-async-interface>` for more details.
  133. * The :ref:`password validator classes <included-password-validators>`
  134. now have a new method ``get_error_message()``, which can be overridden in
  135. subclasses to customize the error messages.
  136. :mod:`django.contrib.contenttypes`
  137. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  138. * ...
  139. :mod:`django.contrib.gis`
  140. ~~~~~~~~~~~~~~~~~~~~~~~~~
  141. * GDAL now supports curved geometries ``CurvePolygon``, ``CompoundCurve``,
  142. ``CircularString``, ``MultiSurface``, and ``MultiCurve`` via the new
  143. :attr:`.OGRGeometry.has_curve` property, and the
  144. :meth:`.OGRGeometry.get_linear_geometry` and
  145. :meth:`.OGRGeometry.get_curve_geometry` methods.
  146. * :lookup:`coveredby` and :lookup:`covers` lookup are now supported on MySQL.
  147. * :lookup:`coveredby` and :lookup:`isvalid` lookups,
  148. :class:`~django.contrib.gis.db.models.Collect` aggregation, and
  149. :class:`~django.contrib.gis.db.models.functions.GeoHash` and
  150. :class:`~django.contrib.gis.db.models.functions.IsValid` database functions
  151. are now supported on MariaDB 11.7+.
  152. :mod:`django.contrib.messages`
  153. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  154. * ...
  155. :mod:`django.contrib.postgres`
  156. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  157. * ...
  158. :mod:`django.contrib.redirects`
  159. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  160. * ...
  161. :mod:`django.contrib.sessions`
  162. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  163. * ...
  164. :mod:`django.contrib.sitemaps`
  165. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  166. * ...
  167. :mod:`django.contrib.sites`
  168. ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  169. * ...
  170. :mod:`django.contrib.staticfiles`
  171. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  172. * ...
  173. :mod:`django.contrib.syndication`
  174. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  175. * All :class:`~django.utils.feedgenerator.SyndicationFeed` classes now support
  176. a ``stylesheets`` attribute. If specified, an ``<? xml-stylesheet ?>``
  177. processing instruction will be added to the top of the document for each
  178. stylesheet in the given list. See :ref:`feed-stylesheets` for more details.
  179. Asynchronous views
  180. ~~~~~~~~~~~~~~~~~~
  181. * ...
  182. Cache
  183. ~~~~~
  184. * ...
  185. CSRF
  186. ~~~~
  187. * ...
  188. Database backends
  189. ~~~~~~~~~~~~~~~~~
  190. * MySQL connections now default to using the ``utf8mb4`` character set,
  191. instead of ``utf8``, which is an alias for the deprecated character set
  192. ``utf8mb3``.
  193. * Oracle backends now support :ref:`connection pools <oracle-pool>`, by setting
  194. ``"pool"`` in the :setting:`OPTIONS` part of your database configuration.
  195. Decorators
  196. ~~~~~~~~~~
  197. * :func:`~django.utils.decorators.method_decorator` now supports wrapping
  198. asynchronous view methods.
  199. Email
  200. ~~~~~
  201. * Tuple items of :class:`EmailMessage.attachments
  202. <django.core.mail.EmailMessage>` and
  203. :class:`EmailMultiAlternatives.attachments
  204. <django.core.mail.EmailMultiAlternatives>` are now named tuples, as opposed
  205. to regular tuples.
  206. * :attr:`EmailMultiAlternatives.alternatives
  207. <django.core.mail.EmailMultiAlternatives.alternatives>` is now a list of
  208. named tuples, as opposed to regular tuples.
  209. * The new :meth:`~django.core.mail.EmailMultiAlternatives.body_contains` method
  210. returns a boolean indicating whether a provided text is contained in the
  211. email ``body`` and in all attached MIME type ``text/*`` alternatives.
  212. Error Reporting
  213. ~~~~~~~~~~~~~~~
  214. * The attribute :attr:`.SafeExceptionReporterFilter.hidden_settings` now
  215. treats values as sensitive if their name includes ``AUTH``.
  216. File Storage
  217. ~~~~~~~~~~~~
  218. * ...
  219. File Uploads
  220. ~~~~~~~~~~~~
  221. * ...
  222. Forms
  223. ~~~~~
  224. * The new :class:`~django.forms.ColorInput` form widget is for entering a color
  225. in ``rrggbb`` hexadecimal format and renders as ``<input type="color" ...>``.
  226. Some browsers support a visual color picker interface for this input type.
  227. * The new :class:`~django.forms.SearchInput` form widget is for entering search
  228. queries and renders as ``<input type="search" ...>``.
  229. * The new :class:`~django.forms.TelInput` form widget is for entering telephone
  230. numbers and renders as ``<input type="tel" ...>``.
  231. * The new ``field_id`` argument for :class:`~django.forms.ErrorList` allows an
  232. HTML ``id`` attribute to be added in the error template. See
  233. :attr:`.ErrorList.field_id` for details.
  234. * An :attr:`~django.forms.BoundField.aria_describedby` property is added to
  235. ``BoundField`` to ease use of this HTML attribute in templates.
  236. * To improve accessibility for screen reader users ``aria-describedby`` is used
  237. to associated form fields with their error messages. See
  238. :ref:`how form errors are displayed <form-error-display>` for details.
  239. * The new asset object :class:`~django.forms.Script` is available for adding
  240. custom HTML-attributes to JavaScript in form media. See
  241. :ref:`paths as objects <form-media-asset-objects>` for more details.
  242. Generic Views
  243. ~~~~~~~~~~~~~
  244. * ...
  245. Internationalization
  246. ~~~~~~~~~~~~~~~~~~~~
  247. * ...
  248. Logging
  249. ~~~~~~~
  250. * ...
  251. Management Commands
  252. ~~~~~~~~~~~~~~~~~~~
  253. * A new warning is displayed when running :djadmin:`runserver`, indicating that
  254. it is unsuitable for production. This warning can be suppressed by setting
  255. the :envvar:`HIDE_PRODUCTION_WARNING` environment variable to ``"true"``.
  256. * The :djadmin:`makemigrations` and :djadmin:`migrate` commands have a new
  257. ``Command.autodetector`` attribute for subclasses to override in order to use
  258. a custom autodetector class.
  259. * The new :meth:`.BaseCommand.get_check_kwargs` method can be overridden in
  260. custom commands to control the running of system checks, e.g. to opt into
  261. database-dependent checks.
  262. Migrations
  263. ~~~~~~~~~~
  264. * The new operation :class:`.AlterConstraint` is a no-op operation that alters
  265. constraints without dropping and recreating constraints in the database.
  266. Models
  267. ~~~~~~
  268. * The ``SELECT`` clause generated when using :meth:`.QuerySet.values` and
  269. :meth:`.QuerySet.values_list` now matches the specified order of the
  270. referenced expressions. Previously, the order was based of a set of
  271. counterintuitive rules which made query combination through methods such as
  272. :meth:`.QuerySet.union` unpredictable.
  273. * Added support for validation of model constraints which use a
  274. :class:`~django.db.models.GeneratedField`.
  275. * The new :attr:`.Expression.set_returning` attribute specifies that the
  276. expression contains a set-returning function, enforcing subquery evaluation.
  277. This is necessary for many Postgres set-returning functions.
  278. * :attr:`CharField.max_length <django.db.models.CharField.max_length>` is no
  279. longer required to be set on SQLite, which supports unlimited ``VARCHAR``
  280. columns.
  281. * :meth:`.QuerySet.explain` now supports the ``memory`` and ``serialize``
  282. options on PostgreSQL 17+.
  283. * The new :class:`~django.db.models.functions.JSONArray` database function
  284. accepts a list of field names or expressions and returns a JSON array
  285. containing those values.
  286. * The new :attr:`.Expression.allows_composite_expressions` attribute specifies
  287. that the expression allows composite expressions, for example, to support
  288. :ref:`composite primary keys <cpk-and-database-functions>`.
  289. Requests and Responses
  290. ~~~~~~~~~~~~~~~~~~~~~~
  291. * The new :attr:`.HttpResponse.text` property provides the string
  292. representation of :attr:`.HttpResponse.content`.
  293. * The new :meth:`.HttpRequest.get_preferred_type` method can be used to query
  294. the preferred media type the client accepts.
  295. * The new ``preserve_request`` argument for
  296. :class:`~django.http.HttpResponseRedirect` and
  297. :class:`~django.http.HttpResponsePermanentRedirect`
  298. determines whether the HTTP status codes 302/307 or 301/308 are used,
  299. respectively.
  300. * The new ``preserve_request`` argument for
  301. :func:`~django.shortcuts.redirect` allows to instruct the user agent to reuse
  302. the HTTP method and body during redirection using specific status codes.
  303. Security
  304. ~~~~~~~~
  305. * ...
  306. Serialization
  307. ~~~~~~~~~~~~~
  308. * Each serialization format now defines a ``Deserializer`` class, rather than a
  309. function, to improve extensibility when defining a
  310. :ref:`custom serialization format <custom-serialization-formats>`.
  311. Signals
  312. ~~~~~~~
  313. * ...
  314. Templates
  315. ~~~~~~~~~
  316. * The new :meth:`~django.template.Library.simple_block_tag` decorator enables
  317. the creation of simple block tags, which can accept and use a section of the
  318. template.
  319. Tests
  320. ~~~~~
  321. * Stack frames from Django's custom assertions are now hidden. This makes test
  322. failures easier to read and enables :option:`test --pdb` to directly enter
  323. into the failing test method.
  324. * Data loaded from :attr:`~django.test.TransactionTestCase.fixtures` and from
  325. migrations enabled with :ref:`serialized_rollback=True
  326. <test-case-serialized-rollback>` are now available during
  327. ``TransactionTestCase.setUpClass()``.
  328. URLs
  329. ~~~~
  330. * :func:`~django.urls.reverse` now accepts ``query`` and ``fragment`` keyword
  331. arguments, allowing the addition of a query string and/or fragment identifier
  332. in the generated URL, respectively.
  333. Utilities
  334. ~~~~~~~~~
  335. * :class:`~django.utils.safestring.SafeString` now returns
  336. :py:data:`NotImplemented` in ``__add__`` for non-string right-hand side
  337. values. This aligns with the :py:class:`str` addition behavior and allows
  338. ``__radd__`` to be used if available.
  339. * :func:`~django.utils.html.format_html_join` now supports taking an iterable
  340. of mappings, passing their contents as keyword arguments to
  341. :func:`~django.utils.html.format_html`.
  342. Validators
  343. ~~~~~~~~~~
  344. * ...
  345. .. _backwards-incompatible-5.2:
  346. Backwards incompatible changes in 5.2
  347. =====================================
  348. Database backend API
  349. --------------------
  350. This section describes changes that may be needed in third-party database
  351. backends.
  352. * The new :meth:`Model._is_pk_set() <django.db.models.Model._is_pk_set>` method
  353. allows checking if a Model instance's primary key is defined.
  354. * ``BaseDatabaseOperations.adapt_decimalfield_value()`` is now a no-op, simply
  355. returning the given value.
  356. :mod:`django.contrib.gis`
  357. -------------------------
  358. * Support for PostGIS 3.0 is removed.
  359. * Support for GDAL 3.0 is removed.
  360. Dropped support for PostgreSQL 13
  361. ---------------------------------
  362. Upstream support for PostgreSQL 13 ends in November 2025. Django 5.2 supports
  363. PostgreSQL 14 and higher.
  364. Changed MySQL connection character set default
  365. ----------------------------------------------
  366. MySQL connections now default to using the ``utf8mb4`` character set, instead
  367. of ``utf8``, which is an alias for the deprecated character set ``utf8mb3``.
  368. ``utf8mb3`` can be specified in the ``OPTIONS`` part of the ``DATABASES``
  369. setting, if needed for legacy databases.
  370. Miscellaneous
  371. -------------
  372. * Adding :attr:`.EmailMultiAlternatives.alternatives` is now only supported via
  373. the :meth:`~.EmailMultiAlternatives.attach_alternative` method.
  374. * The minimum supported version of ``gettext`` is increased from 0.15 to 0.19.
  375. * ``HttpRequest.accepted_types`` is now sorted by the client's preference,
  376. based on the request's ``Accept`` header.
  377. * The attributes :attr:`.UniqueConstraint.violation_error_code` and
  378. :attr:`.UniqueConstraint.violation_error_message` are now always used when
  379. provided. Previously, they were ignored if :attr:`.UniqueConstraint.fields`
  380. was set without a :attr:`.UniqueConstraint.condition`.
  381. * The :func:`~django.template.context_processors.debug` context processor is no
  382. longer included in the default project template.
  383. * The following methods now have ``alters_data=True`` set to prevent side
  384. effects when :ref:`rendering a template context <alters-data-description>`:
  385. * :meth:`.UserManager.create_user`
  386. * :meth:`.UserManager.acreate_user`
  387. * :meth:`.UserManager.create_superuser`
  388. * :meth:`.UserManager.acreate_superuser`
  389. * :meth:`.QuerySet.create`
  390. * :meth:`.QuerySet.acreate`
  391. * :meth:`.QuerySet.bulk_create`
  392. * :meth:`.QuerySet.abulk_create`
  393. * :meth:`.QuerySet.get_or_create`
  394. * :meth:`.QuerySet.aget_or_create`
  395. * :meth:`.QuerySet.update_or_create`
  396. * :meth:`.QuerySet.aupdate_or_create`
  397. * The minimum supported version of ``oracledb`` is increased from 1.3.2 to
  398. 2.3.0.
  399. * Built-in aggregate functions accepting only one argument (``Avg``, ``Count``,
  400. ``Max``, ``Min``, ``StdDev``, ``Sum``, and ``Variance``) now raise
  401. :exc:`TypeError` when called with an incorrect number of arguments.
  402. .. _deprecated-features-5.2:
  403. Features deprecated in 5.2
  404. ==========================
  405. Miscellaneous
  406. -------------
  407. * The ``all`` argument for the ``django.contrib.staticfiles.finders.find()``
  408. function is deprecated in favor of the ``find_all`` argument.
  409. * The fallback to ``request.user`` when ``user`` is ``None`` in
  410. ``django.contrib.auth.login()`` and ``django.contrib.auth.alogin()`` will be
  411. removed.
  412. * The ``ordering`` keyword argument of the PostgreSQL specific aggregation
  413. functions ``django.contrib.postgres.aggregates.ArrayAgg``,
  414. ``django.contrib.postgres.aggregates.JSONBAgg``, and
  415. ``django.contrib.postgres.aggregates.StringAgg`` is deprecated in favor
  416. of the ``order_by`` argument.