4.1.txt 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  1. ============================================
  2. Django 4.1 release notes - UNDER DEVELOPMENT
  3. ============================================
  4. *Expected August 2022*
  5. Welcome to Django 4.1!
  6. These release notes cover the :ref:`new features <whats-new-4.1>`, as well as
  7. some :ref:`backwards incompatible changes <backwards-incompatible-4.1>` you'll
  8. want to be aware of when upgrading from Django 4.0 or earlier. We've
  9. :ref:`begun the deprecation process for some features
  10. <deprecated-features-4.1>`.
  11. See the :doc:`/howto/upgrade-version` guide if you're updating an existing
  12. project.
  13. Python compatibility
  14. ====================
  15. Django 4.1 supports Python 3.8, 3.9, and 3.10. We **highly recommend** and only
  16. officially support the latest release of each series.
  17. .. _whats-new-4.1:
  18. What's new in Django 4.1
  19. ========================
  20. .. _csrf-cookie-masked-usage:
  21. ``CSRF_COOKIE_MASKED`` setting
  22. ------------------------------
  23. The new :setting:`CSRF_COOKIE_MASKED` transitional setting allows specifying
  24. whether to mask the CSRF cookie.
  25. :class:`~django.middleware.csrf.CsrfViewMiddleware` no longer masks the CSRF
  26. cookie like it does the CSRF token in the DOM. If you are upgrading multiple
  27. instances of the same project to Django 4.1, you should set
  28. :setting:`CSRF_COOKIE_MASKED` to ``True`` during the transition, in
  29. order to allow compatibility with the older versions of Django. Once the
  30. transition to 4.1 is complete you can stop overriding
  31. :setting:`CSRF_COOKIE_MASKED`.
  32. This setting is deprecated as of this release and will be removed in Django
  33. 5.0.
  34. Minor features
  35. --------------
  36. :mod:`django.contrib.admin`
  37. ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  38. * The admin :ref:`dark mode CSS variables <admin-theming>` are now applied in a
  39. separate stylesheet and template block.
  40. * :ref:`modeladmin-list-filters` providing custom ``FieldListFilter``
  41. subclasses can now control the query string value separator when filtering
  42. for multiple values using the ``__in`` lookup.
  43. * The admin :meth:`history view <django.contrib.admin.ModelAdmin.history_view>`
  44. is now paginated.
  45. * Related widget wrappers now have a link to object's change form.
  46. * The :meth:`.AdminSite.get_app_list` method now allows changing the order of
  47. apps and models on the admin index page.
  48. :mod:`django.contrib.admindocs`
  49. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  50. * ...
  51. :mod:`django.contrib.auth`
  52. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  53. * The default iteration count for the PBKDF2 password hasher is increased from
  54. 320,000 to 390,000.
  55. * The :meth:`.RemoteUserBackend.configure_user` method now allows synchronizing
  56. user attributes with attributes in a remote system such as an LDAP directory.
  57. :mod:`django.contrib.contenttypes`
  58. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  59. * ...
  60. :mod:`django.contrib.gis`
  61. ~~~~~~~~~~~~~~~~~~~~~~~~~
  62. * The new :meth:`.GEOSGeometry.make_valid()` method allows converting invalid
  63. geometries to valid ones.
  64. :mod:`django.contrib.messages`
  65. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  66. * ...
  67. :mod:`django.contrib.postgres`
  68. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  69. * The new :class:`BitXor() <django.contrib.postgres.aggregates.BitXor>`
  70. aggregate function returns an ``int`` of the bitwise ``XOR`` of all non-null
  71. input values.
  72. * :class:`~django.contrib.postgres.indexes.SpGistIndex` now supports covering
  73. indexes on PostgreSQL 14+.
  74. * :class:`~django.contrib.postgres.constraints.ExclusionConstraint` now
  75. supports covering exclusion constraints using SP-GiST indexes on PostgreSQL
  76. 14+.
  77. * The new ``default_bounds`` attribute of :attr:`DateTimeRangeField
  78. <django.contrib.postgres.fields.DateTimeRangeField.default_bounds>` and
  79. :attr:`DecimalRangeField
  80. <django.contrib.postgres.fields.DecimalRangeField.default_bounds>` allows
  81. specifying bounds for list and tuple inputs.
  82. * :class:`~django.contrib.postgres.constraints.ExclusionConstraint` now allows
  83. specifying operator classes with the
  84. :class:`OpClass() <django.contrib.postgres.indexes.OpClass>` expression.
  85. :mod:`django.contrib.redirects`
  86. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  87. * ...
  88. :mod:`django.contrib.sessions`
  89. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  90. * ...
  91. :mod:`django.contrib.sitemaps`
  92. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  93. * The default sitemap index template ``<sitemapindex>`` now includes the
  94. ``<lastmod>`` timestamp where available, through the new
  95. :meth:`~django.contrib.sitemaps.Sitemap.get_latest_lastmod` method. Custom
  96. sitemap index templates should be updated for the adjusted :ref:`context
  97. variables <sitemap-index-context-variables>`.
  98. :mod:`django.contrib.sites`
  99. ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  100. * ...
  101. :mod:`django.contrib.staticfiles`
  102. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  103. * :class:`~django.contrib.staticfiles.storage.ManifestStaticFilesStorage` now
  104. replaces paths to CSS source map references with their hashed counterparts.
  105. :mod:`django.contrib.syndication`
  106. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  107. * ...
  108. Cache
  109. ~~~~~
  110. * ...
  111. CSRF
  112. ~~~~
  113. * ...
  114. Database backends
  115. ~~~~~~~~~~~~~~~~~
  116. * Third-party database backends can now specify the minimum required version of
  117. the database using the ``DatabaseFeatures.minimum_database_version``
  118. attribute which is a tuple (e.g. ``(10, 0)`` means "10.0"). If a minimum
  119. version is specified, backends must also implement
  120. ``DatabaseWrapper.get_database_version()``, which returns a tuple of the
  121. current database version. The backend's
  122. ``DatabaseWrapper.init_connection_state()`` method must call ``super()`` in
  123. order for the check to run.
  124. Decorators
  125. ~~~~~~~~~~
  126. * ...
  127. Email
  128. ~~~~~
  129. * ...
  130. Error Reporting
  131. ~~~~~~~~~~~~~~~
  132. * ...
  133. File Storage
  134. ~~~~~~~~~~~~
  135. * ...
  136. File Uploads
  137. ~~~~~~~~~~~~
  138. * ...
  139. Forms
  140. ~~~~~
  141. * The new :meth:`~django.forms.BoundField.legend_tag` allows rendering field
  142. labels in ``<legend>`` tags via the new ``tag`` argument of
  143. :meth:`~django.forms.BoundField.label_tag`.
  144. * The new ``edit_only`` argument for :func:`.modelformset_factory` and
  145. :func:`.inlineformset_factory` allows preventing new objects creation.
  146. * The ``js`` and ``css`` class attributes of :doc:`Media </topics/forms/media>`
  147. now allow using hashable objects, not only path strings, as long as those
  148. objects implement the ``__html__()`` method (typically when decorated with
  149. the :func:`~django.utils.html.html_safe` decorator).
  150. Generic Views
  151. ~~~~~~~~~~~~~
  152. * ...
  153. Internationalization
  154. ~~~~~~~~~~~~~~~~~~~~
  155. * The :func:`~django.conf.urls.i18n.i18n_patterns` function now supports
  156. languages with both scripts and regions.
  157. Logging
  158. ~~~~~~~
  159. * ...
  160. Management Commands
  161. ~~~~~~~~~~~~~~~~~~~
  162. * :option:`makemigrations --no-input` now logs default answers and reasons why
  163. migrations cannot be created.
  164. * The new :option:`makemigrations --scriptable` option diverts log output and
  165. input prompts to ``stderr``, writing only paths of generated migration files
  166. to ``stdout``.
  167. * The new :option:`migrate --prune` option allows deleting nonexistent
  168. migrations from the ``django_migrations`` table.
  169. * Python files created by :djadmin:`startproject`, :djadmin:`startapp`,
  170. :djadmin:`optimizemigration`, :djadmin:`makemigrations`, and
  171. :djadmin:`squashmigrations` are now formatted using the ``black`` command if
  172. it is present on your ``PATH``.
  173. * The new :djadmin:`optimizemigration` command allows optimizing operations for
  174. a migration.
  175. Migrations
  176. ~~~~~~~~~~
  177. * ...
  178. Models
  179. ~~~~~~
  180. * The ``order_by`` argument of the
  181. :class:`~django.db.models.expressions.Window` expression now accepts string
  182. references to fields and transforms.
  183. * The new :setting:`CONN_HEALTH_CHECKS` setting allows enabling health checks
  184. for :ref:`persistent database connections <persistent-database-connections>`
  185. in order to reduce the number of failed requests, e.g. after database server
  186. restart.
  187. * :meth:`.QuerySet.bulk_create` now supports updating fields when a row
  188. insertion fails uniqueness constraints. This is supported on MariaDB, MySQL,
  189. PostgreSQL, and SQLite 3.24+.
  190. * :meth:`.QuerySet.iterator` now supports prefetching related objects as long
  191. as the ``chunk_size`` argument is provided. In older versions, no prefetching
  192. was done.
  193. * :class:`~django.db.models.Q` objects and querysets can now be combined using
  194. ``^`` as the exclusive or (``XOR``) operator. ``XOR`` is natively supported
  195. on MariaDB and MySQL. For databases that do not support ``XOR``, the query
  196. will be converted to an equivalent using ``AND``, ``OR``, and ``NOT``.
  197. Requests and Responses
  198. ~~~~~~~~~~~~~~~~~~~~~~
  199. * :meth:`.HttpResponse.set_cookie` now supports :class:`~datetime.timedelta`
  200. objects for the ``max_age`` argument.
  201. Security
  202. ~~~~~~~~
  203. * The new :setting:`SECRET_KEY_FALLBACKS` setting allows providing a list of
  204. values for secret key rotation.
  205. * The :setting:`SECURE_PROXY_SSL_HEADER` setting now supports a comma-separated
  206. list of protocols in the header value.
  207. Serialization
  208. ~~~~~~~~~~~~~
  209. * ...
  210. Signals
  211. ~~~~~~~
  212. * The :data:`~django.db.models.signals.pre_delete` and
  213. :data:`~django.db.models.signals.post_delete` signals now dispatch the
  214. ``origin`` of the deletion.
  215. Templates
  216. ~~~~~~~~~
  217. * :tfilter:`json_script` template filter now allows wrapping in a ``<script>``
  218. tag without the HTML ``id`` attribute.
  219. Tests
  220. ~~~~~
  221. * A nested atomic block marked as durable in :class:`django.test.TestCase` now
  222. raises a ``RuntimeError``, the same as outside of tests.
  223. URLs
  224. ~~~~
  225. * The new :attr:`.ResolverMatch.captured_kwargs` attribute stores the captured
  226. keyword arguments, as parsed from the URL.
  227. * The new :attr:`.ResolverMatch.extra_kwargs` attribute stores the additional
  228. keyword arguments passed to the view function.
  229. Utilities
  230. ~~~~~~~~~
  231. * ``SimpleLazyObject`` now supports addition operations.
  232. * :func:`~django.utils.safestring.mark_safe` now preserves lazy objects.
  233. Validators
  234. ~~~~~~~~~~
  235. * ...
  236. .. _backwards-incompatible-4.1:
  237. Backwards incompatible changes in 4.1
  238. =====================================
  239. Database backend API
  240. --------------------
  241. This section describes changes that may be needed in third-party database
  242. backends.
  243. * ``BaseDatabaseFeatures.has_case_insensitive_like`` is changed from ``True``
  244. to ``False`` to reflect the behavior of most databases.
  245. * ``DatabaseIntrospection.get_key_columns()`` is removed. Use
  246. ``DatabaseIntrospection.get_relations()`` instead.
  247. * ``DatabaseOperations.ignore_conflicts_suffix_sql()`` method is replaced by
  248. ``DatabaseOperations.on_conflict_suffix_sql()`` that accepts the ``fields``,
  249. ``on_conflict``, ``update_fields``, and ``unique_fields`` arguments.
  250. * The ``ignore_conflicts`` argument of the
  251. ``DatabaseOperations.insert_statement()`` method is replaced by
  252. ``on_conflict`` that accepts ``django.db.models.constants.OnConflict``.
  253. :mod:`django.contrib.gis`
  254. -------------------------
  255. * Support for GDAL 2.1 is removed.
  256. Dropped support for MariaDB 10.2
  257. --------------------------------
  258. Upstream support for MariaDB 10.2 ends in May 2022. Django 4.1 supports MariaDB
  259. 10.3 and higher.
  260. Admin changelist searches spanning multi-valued relationships changes
  261. ---------------------------------------------------------------------
  262. Admin changelist searches using multiple search terms are now applied in a
  263. single call to ``filter()``, rather than in sequential ``filter()`` calls.
  264. For multi-valued relationships, this means that rows from the related model
  265. must match all terms rather than any term. For example, if ``search_fields``
  266. is set to ``['child__name', 'child__age']``, and a user searches for
  267. ``'Jamal 17'``, parent rows will be returned only if there is a relationship to
  268. some 17-year-old child named Jamal, rather than also returning parents who
  269. merely have a younger or older child named Jamal in addition to some other
  270. 17-year-old.
  271. See the :ref:`spanning-multi-valued-relationships` topic for more discussion of
  272. this difference. In Django 4.0 and earlier,
  273. :meth:`~django.contrib.admin.ModelAdmin.get_search_results` followed the
  274. second example query, but this undocumented behavior led to queries with
  275. excessive joins.
  276. Reverse foreign key changes for unsaved model instances
  277. -------------------------------------------------------
  278. In order to unify the behavior with many-to-many relations for unsaved model
  279. instances, a reverse foreign key now raises ``ValueError`` when calling
  280. :class:`related managers <django.db.models.fields.related.RelatedManager>` for
  281. unsaved objects.
  282. Miscellaneous
  283. -------------
  284. * Related managers for :class:`~django.db.models.ForeignKey`,
  285. :class:`~django.db.models.ManyToManyField`, and
  286. :class:`~django.contrib.contenttypes.fields.GenericRelation` are now cached
  287. on the :class:`~django.db.models.Model` instance to which they belong.
  288. * The Django test runner now returns a non-zero error code for unexpected
  289. successes from tests marked with :py:func:`unittest.expectedFailure`.
  290. * :class:`~django.middleware.csrf.CsrfViewMiddleware` no longer masks the CSRF
  291. cookie like it does the CSRF token in the DOM.
  292. * :class:`~django.middleware.csrf.CsrfViewMiddleware` now uses
  293. ``request.META['CSRF_COOKIE']`` for storing the unmasked CSRF secret rather
  294. than a masked version. This is an undocumented, private API.
  295. * The :attr:`.ModelAdmin.actions` and
  296. :attr:`~django.contrib.admin.ModelAdmin.inlines` attributes now default to an
  297. empty tuple rather than an empty list to discourage unintended mutation.
  298. * The ``type="text/css"`` attribute is no longer included in ``<link>`` tags
  299. for CSS :doc:`form media </topics/forms/media>`.
  300. * ``formset:added`` and ``formset:removed`` JavaScript events are now pure
  301. JavaScript events and don't depend on jQuery. See
  302. :ref:`admin-javascript-inline-form-events` for more details on the change.
  303. * The ``exc_info`` argument of the undocumented
  304. ``django.utils.log.log_response()`` function is replaced by ``exception``.
  305. * The ``size`` argument of the undocumented
  306. ``django.views.static.was_modified_since()`` function is removed.
  307. * The admin log out UI now uses ``POST`` requests.
  308. .. _deprecated-features-4.1:
  309. Features deprecated in 4.1
  310. ==========================
  311. Log out via GET
  312. ---------------
  313. Logging out via ``GET`` requests to the :py:class:`built-in logout view
  314. <django.contrib.auth.views.LogoutView>` is deprecated. Use ``POST`` requests
  315. instead.
  316. If you want to retain the user experience of an HTML link, you can use a form
  317. that is styled to appear as a link:
  318. .. code-block:: html
  319. <form id="logout-form" method="post" action="{% url 'admin:logout' %}">
  320. {% csrf_token %}
  321. <button type="submit">{% translate "Log out" %}</button>
  322. </form>
  323. .. code-block:: css
  324. #logout-form {
  325. display: inline;
  326. }
  327. #logout-form button {
  328. background: none;
  329. border: none;
  330. cursor: pointer;
  331. padding: 0;
  332. text-decoration: underline;
  333. }
  334. Miscellaneous
  335. -------------
  336. * The context for sitemap index templates of a flat list of URLs is deprecated.
  337. Custom sitemap index templates should be updated for the adjusted
  338. :ref:`context variables <sitemap-index-context-variables>`, expecting a list
  339. of objects with ``location`` and optional ``lastmod`` attributes.
  340. * ``CSRF_COOKIE_MASKED`` transitional setting is deprecated.
  341. * The ``name`` argument of :func:`django.utils.functional.cached_property` is
  342. deprecated as it's unnecessary as of Python 3.6.
  343. * The ``opclasses`` argument of
  344. ``django.contrib.postgres.constraints.ExclusionConstraint`` is deprecated in
  345. favor of using :class:`OpClass() <django.contrib.postgres.indexes.OpClass>`
  346. in :attr:`.ExclusionConstraint.expressions`. To use it, you need to add
  347. ``'django.contrib.postgres'`` in your :setting:`INSTALLED_APPS`.
  348. After making this change, :djadmin:`makemigrations` will generate a new
  349. migration with two operations: ``RemoveConstraint`` and ``AddConstraint``.
  350. Since this change has no effect on the database schema,
  351. the :class:`~django.db.migrations.operations.SeparateDatabaseAndState`
  352. operation can be used to only update the migration state without running any
  353. SQL. Move the generated operations into the ``state_operations`` argument of
  354. :class:`~django.db.migrations.operations.SeparateDatabaseAndState`. For
  355. example::
  356. class Migration(migrations.Migration):
  357. ...
  358. operations = [
  359. migrations.SeparateDatabaseAndState(
  360. database_operations=[],
  361. state_operations=[
  362. migrations.RemoveConstraint(
  363. ...
  364. ),
  365. migrations.AddConstraint(
  366. ...
  367. ),
  368. ],
  369. ),
  370. ]
  371. * The undocumented ability to pass ``errors=None`` to
  372. :meth:`.SimpleTestCase.assertFormError` and
  373. :meth:`~.SimpleTestCase.assertFormsetError` is deprecated. Use ``errors=[]``
  374. instead.
  375. * ``django.contrib.sessions.serializers.PickleSerializer`` is deprecated due to
  376. the risk of remote code execution.
  377. * The usage of ``QuerySet.iterator()`` on a queryset that prefetches related
  378. objects without providing the ``chunk_size`` argument is deprecated. In older
  379. versions, no prefetching was done. Providing a value for ``chunk_size``
  380. signifies that the additional query per chunk needed to prefetch is desired.
  381. * Passing unsaved model instances to related filters is deprecated. In Django
  382. 5.0, the exception will be raised.
  383. * ``created=True`` is added to the signature of
  384. :meth:`.RemoteUserBackend.configure_user`. Support for ``RemoteUserBackend``
  385. subclasses that do not accept this argument is deprecated.
  386. Features removed in 4.1
  387. =======================
  388. These features have reached the end of their deprecation cycle and are removed
  389. in Django 4.1.
  390. See :ref:`deprecated-features-3.2` for details on these changes, including how
  391. to remove usage of these features.
  392. * Support for assigning objects which don't support creating deep copies with
  393. ``copy.deepcopy()`` to class attributes in ``TestCase.setUpTestData()`` is
  394. removed.
  395. * Support for using a boolean value in
  396. :attr:`.BaseCommand.requires_system_checks` is removed.
  397. * The ``whitelist`` argument and ``domain_whitelist`` attribute of
  398. ``django.core.validators.EmailValidator`` are removed.
  399. * The ``default_app_config`` application configuration variable is removed.
  400. * ``TransactionTestCase.assertQuerysetEqual()`` no longer calls ``repr()`` on a
  401. queryset when compared to string values.
  402. * The ``django.core.cache.backends.memcached.MemcachedCache`` backend is
  403. removed.
  404. * Support for the pre-Django 3.2 format of messages used by
  405. ``django.contrib.messages.storage.cookie.CookieStorage`` is removed.