1. ========================
    
  2. Django 3.1 release notes
    
  3. ========================
    
  4. 
    
  5. *August 4, 2020*
    
  6. 
    
  7. Welcome to Django 3.1!
    
  8. 
    
  9. These release notes cover the :ref:`new features <whats-new-3.1>`, as well as
    
  10. some :ref:`backwards incompatible changes <backwards-incompatible-3.1>` you'll
    
  11. want to be aware of when upgrading from Django 3.0 or earlier. We've
    
  12. :ref:`dropped some features<removed-features-3.1>` that have reached the end of
    
  13. their deprecation cycle, and we've :ref:`begun the deprecation process for
    
  14. some features <deprecated-features-3.1>`.
    
  15. 
    
  16. See the :doc:`/howto/upgrade-version` guide if you're updating an existing
    
  17. project.
    
  18. 
    
  19. Python compatibility
    
  20. ====================
    
  21. 
    
  22. Django 3.1 supports Python 3.6, 3.7, 3.8, and 3.9 (as of 3.1.3). We **highly
    
  23. recommend** and only officially support the latest release of each series.
    
  24. 
    
  25. .. _whats-new-3.1:
    
  26. 
    
  27. What's new in Django 3.1
    
  28. ========================
    
  29. 
    
  30. Asynchronous views and middleware support
    
  31. -----------------------------------------
    
  32. 
    
  33. Django now supports a fully asynchronous request path, including:
    
  34. 
    
  35. * :ref:`Asynchronous views <async-views>`
    
  36. * :ref:`Asynchronous middleware <async-middleware>`
    
  37. * :ref:`Asynchronous tests and test client <async-tests>`
    
  38. 
    
  39. To get started with async views, you need to declare a view using
    
  40. ``async def``::
    
  41. 
    
  42.     async def my_view(request):
    
  43.         await asyncio.sleep(0.5)
    
  44.         return HttpResponse('Hello, async world!')
    
  45. 
    
  46. All asynchronous features are supported whether you are running under WSGI or
    
  47. ASGI mode. However, there will be performance penalties using async code in
    
  48. WSGI mode. You can read more about the specifics in :doc:`/topics/async`
    
  49. documentation.
    
  50. 
    
  51. You are free to mix async and sync views, middleware, and tests as much as you
    
  52. want. Django will ensure that you always end up with the right execution
    
  53. context. We expect most projects will keep the majority of their views
    
  54. synchronous, and only have a select few running in async mode - but it is
    
  55. entirely your choice.
    
  56. 
    
  57. Django's ORM, cache layer, and other pieces of code that do long-running
    
  58. network calls do not yet support async access. We expect to add support for
    
  59. them in upcoming releases. Async views are ideal, however, if you are doing a
    
  60. lot of API or HTTP calls inside your view, you can now natively do all those
    
  61. HTTP calls in parallel to considerably speed up your view's execution.
    
  62. 
    
  63. Asynchronous support should be entirely backwards-compatible and we have tried
    
  64. to ensure that it has no speed regressions for your existing, synchronous code.
    
  65. It should have no noticeable effect on any existing Django projects.
    
  66. 
    
  67. JSONField for all supported database backends
    
  68. ---------------------------------------------
    
  69. 
    
  70. Django now includes :class:`.models.JSONField` and
    
  71. :class:`forms.JSONField <django.forms.JSONField>` that can be used on all
    
  72. supported database backends. Both fields support the use of custom JSON
    
  73. encoders and decoders. The model field supports the introspection,
    
  74. :ref:`lookups, and transforms <querying-jsonfield>` that were previously
    
  75. PostgreSQL-only::
    
  76. 
    
  77.     from django.db import models
    
  78. 
    
  79.     class ContactInfo(models.Model):
    
  80.         data = models.JSONField()
    
  81. 
    
  82.     ContactInfo.objects.create(data={
    
  83.         'name': 'John',
    
  84.         'cities': ['London', 'Cambridge'],
    
  85.         'pets': {'dogs': ['Rufus', 'Meg']},
    
  86.     })
    
  87.     ContactInfo.objects.filter(
    
  88.         data__name='John',
    
  89.         data__pets__has_key='dogs',
    
  90.         data__cities__contains='London',
    
  91.     ).delete()
    
  92. 
    
  93. If your project uses ``django.contrib.postgres.fields.JSONField``, plus the
    
  94. related form field and transforms, you should adjust to use the new fields,
    
  95. and generate and apply a database migration. For now, the old fields and
    
  96. transforms are left as a reference to the new ones and are :ref:`deprecated as
    
  97. of this release <deprecated-jsonfield>`.
    
  98. 
    
  99. .. _default-hashing-algorithm-usage:
    
  100. 
    
  101. ``DEFAULT_HASHING_ALGORITHM`` settings
    
  102. --------------------------------------
    
  103. 
    
  104. The new ``DEFAULT_HASHING_ALGORITHM`` transitional setting allows specifying
    
  105. the default hashing algorithm to use for encoding cookies, password reset
    
  106. tokens in the admin site, user sessions, and signatures created by
    
  107. :class:`django.core.signing.Signer` and :meth:`django.core.signing.dumps`.
    
  108. 
    
  109. Support for SHA-256 was added in Django 3.1. If you are upgrading multiple
    
  110. instances of the same project to Django 3.1, you should set
    
  111. ``DEFAULT_HASHING_ALGORITHM`` to ``'sha1'`` during the transition, in order to
    
  112. allow compatibility with the older versions of Django. Note that this requires
    
  113. Django 3.1.1+. Once the transition to 3.1 is complete you can stop overriding
    
  114. ``DEFAULT_HASHING_ALGORITHM``.
    
  115. 
    
  116. This setting is deprecated as of this release, because support for tokens,
    
  117. cookies, sessions, and signatures that use SHA-1 algorithm will be removed in
    
  118. Django 4.0.
    
  119. 
    
  120. Minor features
    
  121. --------------
    
  122. 
    
  123. :mod:`django.contrib.admin`
    
  124. ~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
  125. 
    
  126. * The new ``django.contrib.admin.EmptyFieldListFilter`` for
    
  127.   :attr:`.ModelAdmin.list_filter` allows filtering on empty values (empty
    
  128.   strings and nulls) in the admin changelist view.
    
  129. 
    
  130. * Filters in the right sidebar of the admin changelist view now contain a link
    
  131.   to clear all filters.
    
  132. 
    
  133. * The admin now has a sidebar on larger screens for easier navigation. It is
    
  134.   enabled by default but can be disabled by using a custom ``AdminSite`` and
    
  135.   setting :attr:`.AdminSite.enable_nav_sidebar` to ``False``.
    
  136. 
    
  137.   Rendering the sidebar requires access to the current request in order to set
    
  138.   CSS and ARIA role affordances. This requires using
    
  139.   ``'django.template.context_processors.request'`` in the
    
  140.   ``'context_processors'`` option of :setting:`OPTIONS <TEMPLATES-OPTIONS>`.
    
  141. 
    
  142. * Initially empty ``extra`` inlines can now be removed, in the same way as
    
  143.   dynamically created ones.
    
  144. 
    
  145. * ``XRegExp`` is upgraded from version 2.0.0 to 3.2.0.
    
  146. 
    
  147. * jQuery is upgraded from version 3.4.1 to 3.5.1.
    
  148. 
    
  149. * Select2 library is upgraded from version 4.0.7 to 4.0.13.
    
  150. 
    
  151. :mod:`django.contrib.auth`
    
  152. ~~~~~~~~~~~~~~~~~~~~~~~~~~
    
  153. 
    
  154. * The default iteration count for the PBKDF2 password hasher is increased from
    
  155.   180,000 to 216,000.
    
  156. 
    
  157. * The new :setting:`PASSWORD_RESET_TIMEOUT` setting allows defining the number
    
  158.   of seconds a password reset link is valid for. This is encouraged instead of
    
  159.   the deprecated ``PASSWORD_RESET_TIMEOUT_DAYS`` setting, which will be removed
    
  160.   in Django 4.0.
    
  161. 
    
  162. * The password reset mechanism now uses the SHA-256 hashing algorithm. Support
    
  163.   for tokens that use the old hashing algorithm remains until Django 4.0.
    
  164. 
    
  165. * :meth:`.AbstractBaseUser.get_session_auth_hash` now uses the SHA-256 hashing
    
  166.   algorithm. Support for user sessions that use the old hashing algorithm
    
  167.   remains until Django 4.0.
    
  168. 
    
  169. :mod:`django.contrib.contenttypes`
    
  170. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
  171. 
    
  172. * The new :option:`remove_stale_contenttypes --include-stale-apps` option
    
  173.   allows removing stale content types from previously installed apps that have
    
  174.   been removed from :setting:`INSTALLED_APPS`.
    
  175. 
    
  176. :mod:`django.contrib.gis`
    
  177. ~~~~~~~~~~~~~~~~~~~~~~~~~
    
  178. 
    
  179. * :lookup:`relate` lookup is now supported on MariaDB.
    
  180. 
    
  181. * Added the :attr:`.LinearRing.is_counterclockwise` property.
    
  182. 
    
  183. * :class:`~django.contrib.gis.db.models.functions.AsGeoJSON` is now supported
    
  184.   on Oracle.
    
  185. 
    
  186. * Added the :class:`~django.contrib.gis.db.models.functions.AsWKB` and
    
  187.   :class:`~django.contrib.gis.db.models.functions.AsWKT` functions.
    
  188. 
    
  189. * Added support for PostGIS 3 and GDAL 3.
    
  190. 
    
  191. :mod:`django.contrib.humanize`
    
  192. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
  193. 
    
  194. * :tfilter:`intword` template filter now supports negative integers.
    
  195. 
    
  196. :mod:`django.contrib.postgres`
    
  197. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
  198. 
    
  199. * The new :class:`~django.contrib.postgres.indexes.BloomIndex` class allows
    
  200.   creating ``bloom`` indexes in the database. The new
    
  201.   :class:`~django.contrib.postgres.operations.BloomExtension` migration
    
  202.   operation installs the ``bloom`` extension to add support for this index.
    
  203. 
    
  204. * :meth:`~django.db.models.Model.get_FOO_display` now supports
    
  205.   :class:`~django.contrib.postgres.fields.ArrayField` and
    
  206.   :class:`~django.contrib.postgres.fields.RangeField`.
    
  207. 
    
  208. * The new :lookup:`rangefield.lower_inc`, :lookup:`rangefield.lower_inf`,
    
  209.   :lookup:`rangefield.upper_inc`, and :lookup:`rangefield.upper_inf` lookups
    
  210.   allow querying :class:`~django.contrib.postgres.fields.RangeField` by a bound
    
  211.   type.
    
  212. 
    
  213. * :lookup:`rangefield.contained_by` now supports
    
  214.   :class:`~django.db.models.SmallAutoField`,
    
  215.   :class:`~django.db.models.AutoField`,
    
  216.   :class:`~django.db.models.BigAutoField`,
    
  217.   :class:`~django.db.models.SmallIntegerField`, and
    
  218.   :class:`~django.db.models.DecimalField`.
    
  219. 
    
  220. * :class:`~django.contrib.postgres.search.SearchQuery` now supports
    
  221.   ``'websearch'`` search type on PostgreSQL 11+.
    
  222. 
    
  223. * :class:`SearchQuery.value <django.contrib.postgres.search.SearchQuery>` now
    
  224.   supports query expressions.
    
  225. 
    
  226. * The new :class:`~django.contrib.postgres.search.SearchHeadline` class allows
    
  227.   highlighting search results.
    
  228. 
    
  229. * :lookup:`search` lookup now supports query expressions.
    
  230. 
    
  231. * The new ``cover_density`` parameter of
    
  232.   :class:`~django.contrib.postgres.search.SearchRank` allows ranking by cover
    
  233.   density.
    
  234. 
    
  235. * The new ``normalization`` parameter of
    
  236.   :class:`~django.contrib.postgres.search.SearchRank` allows rank
    
  237.   normalization.
    
  238. 
    
  239. * The new :attr:`.ExclusionConstraint.deferrable` attribute allows creating
    
  240.   deferrable exclusion constraints.
    
  241. 
    
  242. :mod:`django.contrib.sessions`
    
  243. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
  244. 
    
  245. * The :setting:`SESSION_COOKIE_SAMESITE` setting now allows ``'None'`` (string)
    
  246.   value to explicitly state that the cookie is sent with all same-site and
    
  247.   cross-site requests.
    
  248. 
    
  249. :mod:`django.contrib.staticfiles`
    
  250. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
  251. 
    
  252. * The :setting:`STATICFILES_DIRS` setting now supports :class:`pathlib.Path`.
    
  253. 
    
  254. Cache
    
  255. ~~~~~
    
  256. 
    
  257. * The :func:`~django.views.decorators.cache.cache_control` decorator and
    
  258.   :func:`~django.utils.cache.patch_cache_control` method now support multiple
    
  259.   field names in the ``no-cache`` directive for the ``Cache-Control`` header,
    
  260.   according to :rfc:`7234#section-5.2.2.2`.
    
  261. 
    
  262. * :meth:`~django.core.caches.cache.delete` now returns ``True`` if the key was
    
  263.   successfully deleted, ``False`` otherwise.
    
  264. 
    
  265. CSRF
    
  266. ~~~~
    
  267. 
    
  268. * The :setting:`CSRF_COOKIE_SAMESITE` setting now allows ``'None'`` (string)
    
  269.   value to explicitly state that the cookie is sent with all same-site and
    
  270.   cross-site requests.
    
  271. 
    
  272. Email
    
  273. ~~~~~
    
  274. 
    
  275. * The :setting:`EMAIL_FILE_PATH` setting, used by the :ref:`file email backend
    
  276.   <topic-email-file-backend>`, now supports :class:`pathlib.Path`.
    
  277. 
    
  278. Error Reporting
    
  279. ~~~~~~~~~~~~~~~
    
  280. 
    
  281. * :class:`django.views.debug.SafeExceptionReporterFilter` now filters sensitive
    
  282.   values from ``request.META`` in exception reports.
    
  283. 
    
  284. * The new :attr:`.SafeExceptionReporterFilter.cleansed_substitute` and
    
  285.   :attr:`.SafeExceptionReporterFilter.hidden_settings` attributes allow
    
  286.   customization of sensitive settings and ``request.META`` filtering in
    
  287.   exception reports.
    
  288. 
    
  289. * The technical 404 debug view now respects
    
  290.   :setting:`DEFAULT_EXCEPTION_REPORTER_FILTER` when applying settings
    
  291.   filtering.
    
  292. 
    
  293. * The new :setting:`DEFAULT_EXCEPTION_REPORTER` allows providing a
    
  294.   :class:`django.views.debug.ExceptionReporter` subclass to customize exception
    
  295.   report generation. See :ref:`custom-error-reports` for details.
    
  296. 
    
  297. File Storage
    
  298. ~~~~~~~~~~~~
    
  299. 
    
  300. * ``FileSystemStorage.save()`` method now supports :class:`pathlib.Path`.
    
  301. 
    
  302. * :class:`~django.db.models.FileField` and
    
  303.   :class:`~django.db.models.ImageField` now accept a callable for ``storage``.
    
  304.   This allows you to modify the used storage at runtime, selecting different
    
  305.   storages for different environments, for example.
    
  306. 
    
  307. Forms
    
  308. ~~~~~
    
  309. 
    
  310. * :class:`~django.forms.ModelChoiceIterator`, used by
    
  311.   :class:`~django.forms.ModelChoiceField` and
    
  312.   :class:`~django.forms.ModelMultipleChoiceField`, now uses
    
  313.   :class:`~django.forms.ModelChoiceIteratorValue` that can be used by widgets
    
  314.   to access model instances. See :ref:`iterating-relationship-choices` for
    
  315.   details.
    
  316. 
    
  317. * :class:`django.forms.DateTimeField` now accepts dates in a subset of ISO 8601
    
  318.   datetime formats, including optional timezone, e.g. ``2019-10-10T06:47``,
    
  319.   ``2019-10-10T06:47:23+04:00``, or ``2019-10-10T06:47:23Z``. The timezone will
    
  320.   always be retained if provided, with timezone-aware datetimes being returned
    
  321.   even when :setting:`USE_TZ` is ``False``.
    
  322. 
    
  323.   Additionally, ``DateTimeField`` now uses ``DATE_INPUT_FORMATS`` in addition
    
  324.   to ``DATETIME_INPUT_FORMATS`` when converting a field input to a ``datetime``
    
  325.   value.
    
  326. 
    
  327. * :attr:`.MultiWidget.widgets` now accepts a dictionary which allows
    
  328.   customizing subwidget ``name`` attributes.
    
  329. 
    
  330. * The new :attr:`.BoundField.widget_type` property can be used to dynamically
    
  331.   adjust form rendering based upon the widget type.
    
  332. 
    
  333. Internationalization
    
  334. ~~~~~~~~~~~~~~~~~~~~
    
  335. 
    
  336. * The :setting:`LANGUAGE_COOKIE_SAMESITE` setting now allows ``'None'``
    
  337.   (string) value to explicitly state that the cookie is sent with all same-site
    
  338.   and cross-site requests.
    
  339. 
    
  340. * Added support and translations for the Algerian Arabic, Igbo, Kyrgyz, Tajik,
    
  341.   and Turkmen languages.
    
  342. 
    
  343. Management Commands
    
  344. ~~~~~~~~~~~~~~~~~~~
    
  345. 
    
  346. * The new :option:`check --database` option allows specifying database aliases
    
  347.   for running the ``database`` system checks. Previously these checks were
    
  348.   enabled for all configured :setting:`DATABASES` by passing the ``database``
    
  349.   tag to the command.
    
  350. 
    
  351. * The new :option:`migrate --check` option makes the command exit with a
    
  352.   non-zero status when unapplied migrations are detected.
    
  353. 
    
  354. * The new ``returncode`` argument for
    
  355.   :attr:`~django.core.management.CommandError` allows customizing the exit
    
  356.   status for management commands.
    
  357. 
    
  358. * The new :option:`dbshell -- ARGUMENTS <dbshell -->` option allows passing
    
  359.   extra arguments to the command-line client for the database.
    
  360. 
    
  361. * The :djadmin:`flush` and :djadmin:`sqlflush` commands now include SQL to
    
  362.   reset sequences on SQLite.
    
  363. 
    
  364. Models
    
  365. ~~~~~~
    
  366. 
    
  367. * The new :class:`~django.db.models.functions.ExtractIsoWeekDay` function
    
  368.   extracts ISO-8601 week days from :class:`~django.db.models.DateField` and
    
  369.   :class:`~django.db.models.DateTimeField`, and the new :lookup:`iso_week_day`
    
  370.   lookup allows querying by an ISO-8601 day of week.
    
  371. 
    
  372. * :meth:`.QuerySet.explain` now supports:
    
  373. 
    
  374.   * ``TREE`` format on MySQL 8.0.16+,
    
  375.   * ``analyze`` option on MySQL 8.0.18+ and MariaDB.
    
  376. 
    
  377. * Added :class:`~django.db.models.PositiveBigIntegerField` which acts much like
    
  378.   a :class:`~django.db.models.PositiveIntegerField` except that it only allows
    
  379.   values under a certain (database-dependent) limit. Values from ``0`` to
    
  380.   ``9223372036854775807`` are safe in all databases supported by Django.
    
  381. 
    
  382. * The new :class:`~django.db.models.RESTRICT` option for
    
  383.   :attr:`~django.db.models.ForeignKey.on_delete` argument of ``ForeignKey`` and
    
  384.   ``OneToOneField`` emulates the behavior of the SQL constraint ``ON DELETE
    
  385.   RESTRICT``.
    
  386. 
    
  387. * :attr:`.CheckConstraint.check` now supports boolean expressions.
    
  388. 
    
  389. * The :meth:`.RelatedManager.add`, :meth:`~.RelatedManager.create`, and
    
  390.   :meth:`~.RelatedManager.set` methods now accept callables as values in the
    
  391.   ``through_defaults`` argument.
    
  392. 
    
  393. * The new ``is_dst``  parameter of the :meth:`.QuerySet.datetimes` determines
    
  394.   the treatment of nonexistent and ambiguous datetimes.
    
  395. 
    
  396. * The new :class:`~django.db.models.F` expression ``bitxor()`` method allows
    
  397.   :ref:`bitwise XOR operation <using-f-expressions-in-filters>`.
    
  398. 
    
  399. * :meth:`.QuerySet.bulk_create` now sets the primary key on objects when using
    
  400.   MariaDB 10.5+.
    
  401. 
    
  402. * The ``DatabaseOperations.sql_flush()`` method now generates more efficient
    
  403.   SQL on MySQL by using ``DELETE`` instead of ``TRUNCATE`` statements for
    
  404.   tables which don't require resetting sequences.
    
  405. 
    
  406. * SQLite functions are now marked as :py:meth:`deterministic
    
  407.   <sqlite3.Connection.create_function>` on Python 3.8+. This allows using them
    
  408.   in check constraints and partial indexes.
    
  409. 
    
  410. * The new :attr:`.UniqueConstraint.deferrable` attribute allows creating
    
  411.   deferrable unique constraints.
    
  412. 
    
  413. Pagination
    
  414. ~~~~~~~~~~
    
  415. 
    
  416. * :class:`~django.core.paginator.Paginator` can now be iterated over to yield
    
  417.   its pages.
    
  418. 
    
  419. Requests and Responses
    
  420. ~~~~~~~~~~~~~~~~~~~~~~
    
  421. 
    
  422. * If :setting:`ALLOWED_HOSTS` is empty and ``DEBUG=True``, subdomains of
    
  423.   localhost are now allowed in the ``Host`` header, e.g. ``static.localhost``.
    
  424. 
    
  425. * :meth:`.HttpResponse.set_cookie` and :meth:`.HttpResponse.set_signed_cookie`
    
  426.   now allow using ``samesite='None'`` (string) to explicitly state that the
    
  427.   cookie is sent with all same-site and cross-site requests.
    
  428. 
    
  429. * The new :meth:`.HttpRequest.accepts` method returns whether the request
    
  430.   accepts the given MIME type according to the ``Accept`` HTTP header.
    
  431. 
    
  432. .. _whats-new-security-3.1:
    
  433. 
    
  434. Security
    
  435. ~~~~~~~~
    
  436. 
    
  437. * The :setting:`SECURE_REFERRER_POLICY` setting now defaults to
    
  438.   ``'same-origin'``. With this configured,
    
  439.   :class:`~django.middleware.security.SecurityMiddleware` sets the
    
  440.   :ref:`referrer-policy` header to ``same-origin`` on all responses that do not
    
  441.   already have it. This prevents the ``Referer`` header being sent to other
    
  442.   origins. If you need the previous behavior, explicitly set
    
  443.   :setting:`SECURE_REFERRER_POLICY` to ``None``.
    
  444. 
    
  445. * The default algorithm of :class:`django.core.signing.Signer`,
    
  446.   :meth:`django.core.signing.loads`, and :meth:`django.core.signing.dumps` is
    
  447.   changed to the SHA-256. Support for signatures made with the old SHA-1
    
  448.   algorithm remains until Django 4.0.
    
  449. 
    
  450.   Also, the new ``algorithm`` parameter of the
    
  451.   :class:`~django.core.signing.Signer` allows customizing the hashing
    
  452.   algorithm.
    
  453. 
    
  454. Templates
    
  455. ~~~~~~~~~
    
  456. 
    
  457. * The renamed :ttag:`translate` and :ttag:`blocktranslate` template tags are
    
  458.   introduced for internationalization in template code. The older :ttag:`trans`
    
  459.   and :ttag:`blocktrans` template tags aliases continue to work, and will be
    
  460.   retained for the foreseeable future.
    
  461. 
    
  462. * The :ttag:`include` template tag now accepts iterables of template names.
    
  463. 
    
  464. Tests
    
  465. ~~~~~
    
  466. 
    
  467. * :class:`~django.test.SimpleTestCase` now implements the ``debug()`` method to
    
  468.   allow running a test without collecting the result and catching exceptions.
    
  469.   This can be used to support running tests under a debugger.
    
  470. 
    
  471. * The new :setting:`MIGRATE <TEST_MIGRATE>` test database setting allows
    
  472.   disabling of migrations during a test database creation.
    
  473. 
    
  474. * Django test runner now supports a :option:`test --buffer` option to discard
    
  475.   output for passing tests.
    
  476. 
    
  477. * :class:`~django.test.runner.DiscoverRunner` now skips running the system
    
  478.   checks on databases not :ref:`referenced by tests<testing-multi-db>`.
    
  479. 
    
  480. * :class:`~django.test.TransactionTestCase` teardown is now faster on MySQL
    
  481.   due to :djadmin:`flush` command improvements. As a side effect the latter
    
  482.   doesn't automatically reset sequences on teardown anymore. Enable
    
  483.   :attr:`.TransactionTestCase.reset_sequences` if your tests require this
    
  484.   feature.
    
  485. 
    
  486. URLs
    
  487. ~~~~
    
  488. 
    
  489. * :ref:`Path converters <registering-custom-path-converters>` can now raise
    
  490.   ``ValueError`` in ``to_url()`` to indicate no match when reversing URLs.
    
  491. 
    
  492. Utilities
    
  493. ~~~~~~~~~
    
  494. 
    
  495. * :func:`~django.utils.encoding.filepath_to_uri` now supports
    
  496.   :class:`pathlib.Path`.
    
  497. 
    
  498. * :func:`~django.utils.dateparse.parse_duration` now supports comma separators
    
  499.   for decimal fractions in the ISO 8601 format.
    
  500. 
    
  501. * :func:`~django.utils.dateparse.parse_datetime`,
    
  502.   :func:`~django.utils.dateparse.parse_duration`, and
    
  503.   :func:`~django.utils.dateparse.parse_time` now support comma separators for
    
  504.   milliseconds.
    
  505. 
    
  506. Miscellaneous
    
  507. ~~~~~~~~~~~~~
    
  508. 
    
  509. * The SQLite backend now supports :class:`pathlib.Path` for the ``NAME``
    
  510.   setting.
    
  511. 
    
  512. * The ``settings.py`` generated by the :djadmin:`startproject` command now uses
    
  513.   :class:`pathlib.Path` instead of :mod:`os.path` for building filesystem
    
  514.   paths.
    
  515. 
    
  516. * The :setting:`TIME_ZONE <DATABASE-TIME_ZONE>` setting is now allowed on
    
  517.   databases that support time zones.
    
  518. 
    
  519. .. _backwards-incompatible-3.1:
    
  520. 
    
  521. Backwards incompatible changes in 3.1
    
  522. =====================================
    
  523. 
    
  524. Database backend API
    
  525. --------------------
    
  526. 
    
  527. This section describes changes that may be needed in third-party database
    
  528. backends.
    
  529. 
    
  530. * ``DatabaseOperations.fetch_returned_insert_columns()`` now requires an
    
  531.   additional ``returning_params`` argument.
    
  532. 
    
  533. * ``connection.timezone`` property is now ``'UTC'`` by default, or the
    
  534.   :setting:`TIME_ZONE <DATABASE-TIME_ZONE>` when :setting:`USE_TZ` is ``True``
    
  535.   on databases that support time zones. Previously, it was ``None`` on
    
  536.   databases that support time zones.
    
  537. 
    
  538. * ``connection._nodb_connection`` property is changed to the
    
  539.   ``connection._nodb_cursor()`` method and now returns a context manager that
    
  540.   yields a cursor and automatically closes the cursor and connection upon
    
  541.   exiting the ``with`` statement.
    
  542. 
    
  543. * ``DatabaseClient.runshell()`` now requires an additional ``parameters``
    
  544.   argument as a list of extra arguments to pass on to the command-line client.
    
  545. 
    
  546. * The ``sequences`` positional argument of ``DatabaseOperations.sql_flush()``
    
  547.   is replaced by the boolean keyword-only argument ``reset_sequences``. If
    
  548.   ``True``, the sequences of the truncated tables will be reset.
    
  549. 
    
  550. * The ``allow_cascade`` argument of ``DatabaseOperations.sql_flush()`` is now a
    
  551.   keyword-only argument.
    
  552. 
    
  553. * The ``using`` positional argument of
    
  554.   ``DatabaseOperations.execute_sql_flush()`` is removed. The method now uses
    
  555.   the database of the called instance.
    
  556. 
    
  557. * Third-party database backends must implement support for ``JSONField`` or set
    
  558.   ``DatabaseFeatures.supports_json_field`` to ``False``. If storing primitives
    
  559.   is not supported, set ``DatabaseFeatures.supports_primitives_in_json_field``
    
  560.   to ``False``. If there is a true datatype for JSON, set
    
  561.   ``DatabaseFeatures.has_native_json_field`` to ``True``. If
    
  562.   :lookup:`jsonfield.contains` and :lookup:`jsonfield.contained_by` are not
    
  563.   supported, set ``DatabaseFeatures.supports_json_field_contains`` to
    
  564.   ``False``.
    
  565. 
    
  566. * Third party database backends must implement introspection for ``JSONField``
    
  567.   or set ``can_introspect_json_field`` to ``False``.
    
  568. 
    
  569. Dropped support for MariaDB 10.1
    
  570. --------------------------------
    
  571. 
    
  572. Upstream support for MariaDB 10.1 ends in October 2020. Django 3.1 supports
    
  573. MariaDB 10.2 and higher.
    
  574. 
    
  575. ``contrib.admin`` browser support
    
  576. ---------------------------------
    
  577. 
    
  578. The admin no longer supports the legacy Internet Explorer browser. See
    
  579. :ref:`the admin FAQ <admin-browser-support>` for details on supported browsers.
    
  580. 
    
  581. :attr:`AbstractUser.first_name <django.contrib.auth.models.User.first_name>` ``max_length`` increased to 150
    
  582. ------------------------------------------------------------------------------------------------------------
    
  583. 
    
  584. A migration for :attr:`django.contrib.auth.models.User.first_name` is included.
    
  585. If you have a custom user model inheriting from ``AbstractUser``, you'll need
    
  586. to generate and apply a database migration for your user model.
    
  587. 
    
  588. If you want to preserve the 30 character limit for first names, use a custom
    
  589. form::
    
  590. 
    
  591.     from django import forms
    
  592.     from django.contrib.auth.forms import UserChangeForm
    
  593. 
    
  594.     class MyUserChangeForm(UserChangeForm):
    
  595.         first_name = forms.CharField(max_length=30, required=False)
    
  596. 
    
  597. If you wish to keep this restriction in the admin when editing users, set
    
  598. ``UserAdmin.form`` to use this form::
    
  599. 
    
  600.     from django.contrib.auth.admin import UserAdmin
    
  601.     from django.contrib.auth.models import User
    
  602. 
    
  603.     class MyUserAdmin(UserAdmin):
    
  604.         form = MyUserChangeForm
    
  605. 
    
  606.     admin.site.unregister(User)
    
  607.     admin.site.register(User, MyUserAdmin)
    
  608. 
    
  609. Miscellaneous
    
  610. -------------
    
  611. 
    
  612. * The cache keys used by :ttag:`cache` and generated by
    
  613.   :func:`~django.core.cache.utils.make_template_fragment_key` are different
    
  614.   from the keys generated by older versions of Django. After upgrading to
    
  615.   Django 3.1, the first request to any previously cached template fragment will
    
  616.   be a cache miss.
    
  617. 
    
  618. * The logic behind the decision to return a redirection fallback or a 204 HTTP
    
  619.   response from the :func:`~django.views.i18n.set_language` view is now based
    
  620.   on the ``Accept`` HTTP header instead of the ``X-Requested-With`` HTTP header
    
  621.   presence.
    
  622. 
    
  623. * The compatibility imports of ``django.core.exceptions.EmptyResultSet`` in
    
  624.   ``django.db.models.query``, ``django.db.models.sql``, and
    
  625.   ``django.db.models.sql.datastructures`` are removed.
    
  626. 
    
  627. * The compatibility import of ``django.core.exceptions.FieldDoesNotExist`` in
    
  628.   ``django.db.models.fields`` is removed.
    
  629. 
    
  630. * The compatibility imports of ``django.forms.utils.pretty_name()`` and
    
  631.   ``django.forms.boundfield.BoundField`` in ``django.forms.forms`` are removed.
    
  632. 
    
  633. * The compatibility imports of ``Context``, ``ContextPopException``, and
    
  634.   ``RequestContext`` in ``django.template.base`` are removed.
    
  635. 
    
  636. * The compatibility import of
    
  637.   ``django.contrib.admin.helpers.ACTION_CHECKBOX_NAME`` in
    
  638.   ``django.contrib.admin`` is removed.
    
  639. 
    
  640. * The :setting:`STATIC_URL` and :setting:`MEDIA_URL` settings set to relative
    
  641.   paths are now prefixed by the server-provided value of ``SCRIPT_NAME`` (or
    
  642.   ``/`` if not set). This change should not affect settings set to valid URLs
    
  643.   or absolute paths.
    
  644. 
    
  645. * :class:`~django.middleware.http.ConditionalGetMiddleware` no longer adds the
    
  646.   ``ETag`` header to responses with an empty
    
  647.   :attr:`~django.http.HttpResponse.content`.
    
  648. 
    
  649. * ``django.utils.decorators.classproperty()`` decorator is made public and
    
  650.   moved to :class:`django.utils.functional.classproperty()`.
    
  651. 
    
  652. * :tfilter:`floatformat` template filter now outputs (positive) ``0`` for
    
  653.   negative numbers which round to zero.
    
  654. 
    
  655. * :attr:`Meta.ordering <django.db.models.Options.ordering>` and
    
  656.   :attr:`Meta.unique_together <django.db.models.Options.unique_together>`
    
  657.   options on models in ``django.contrib`` modules that were formerly tuples are
    
  658.   now lists.
    
  659. 
    
  660. * The admin calendar widget now handles two-digit years according to the Open
    
  661.   Group Specification, i.e. values between 69 and 99 are mapped to the previous
    
  662.   century, and values between 0 and 68 are mapped to the current century.
    
  663. 
    
  664. * Date-only formats are removed from the default list for
    
  665.   :setting:`DATETIME_INPUT_FORMATS`.
    
  666. 
    
  667. * The :class:`~django.forms.FileInput` widget no longer renders with the
    
  668.   ``required`` HTML attribute when initial data exists.
    
  669. 
    
  670. * The undocumented ``django.views.debug.ExceptionReporterFilter`` class is
    
  671.   removed. As per the :ref:`custom-error-reports` documentation, classes to be
    
  672.   used with :setting:`DEFAULT_EXCEPTION_REPORTER_FILTER` need to inherit from
    
  673.   :class:`django.views.debug.SafeExceptionReporterFilter`.
    
  674. 
    
  675. * The cache timeout set by :func:`~django.views.decorators.cache.cache_page`
    
  676.   decorator now takes precedence over the ``max-age`` directive from the
    
  677.   ``Cache-Control`` header.
    
  678. 
    
  679. * Providing a non-local remote field in the :attr:`.ForeignKey.to_field`
    
  680.   argument now raises :class:`~django.core.exceptions.FieldError`.
    
  681. 
    
  682. * :setting:`SECURE_REFERRER_POLICY` now defaults to ``'same-origin'``. See the
    
  683.   *What's New* :ref:`Security section <whats-new-security-3.1>` above for more
    
  684.   details.
    
  685. 
    
  686. * :djadmin:`check` management command now runs the ``database`` system checks
    
  687.   only for database aliases specified using :option:`check --database` option.
    
  688. 
    
  689. * :djadmin:`migrate` management command now runs the ``database`` system checks
    
  690.   only for a database to migrate.
    
  691. 
    
  692. * The admin CSS classes ``row1`` and ``row2`` are removed in favor of
    
  693.   ``:nth-child(odd)`` and ``:nth-child(even)`` pseudo-classes.
    
  694. 
    
  695. * The :func:`~django.contrib.auth.hashers.make_password` function now requires
    
  696.   its argument to be a string or bytes. Other types should be explicitly cast
    
  697.   to one of these.
    
  698. 
    
  699. * The undocumented ``version`` parameter to the
    
  700.   :class:`~django.contrib.gis.db.models.functions.AsKML` function is removed.
    
  701. 
    
  702. * :ref:`JSON and YAML serializers <serialization-formats>`, used by
    
  703.   :djadmin:`dumpdata`, now dump all data with Unicode by default. If you need
    
  704.   the previous behavior, pass ``ensure_ascii=True`` to JSON serializer, or
    
  705.   ``allow_unicode=False`` to YAML serializer.
    
  706. 
    
  707. * The auto-reloader no longer monitors changes in built-in Django translation
    
  708.   files.
    
  709. 
    
  710. * The minimum supported version of ``mysqlclient`` is increased from 1.3.13 to
    
  711.   1.4.0.
    
  712. 
    
  713. * The undocumented ``django.contrib.postgres.forms.InvalidJSONInput`` and
    
  714.   ``django.contrib.postgres.forms.JSONString`` are moved to
    
  715.   ``django.forms.fields``.
    
  716. 
    
  717. * The undocumented ``django.contrib.postgres.fields.jsonb.JsonAdapter`` class
    
  718.   is removed.
    
  719. 
    
  720. * The :ttag:`{% localize off %} <localize>` tag and :tfilter:`unlocalize`
    
  721.   filter no longer respect :setting:`DECIMAL_SEPARATOR` setting.
    
  722. 
    
  723. * The minimum supported version of ``asgiref`` is increased from 3.2 to
    
  724.   3.2.10.
    
  725. 
    
  726. * The :doc:`Media </topics/forms/media>` class now renders ``<script>`` tags
    
  727.   without the ``type`` attribute to follow `WHATWG recommendations
    
  728.   <https://html.spec.whatwg.org/multipage/scripting.html#the-script-element>`_.
    
  729. 
    
  730. * :class:`~django.forms.ModelChoiceIterator`, used by
    
  731.   :class:`~django.forms.ModelChoiceField` and
    
  732.   :class:`~django.forms.ModelMultipleChoiceField`, now yields 2-tuple choices
    
  733.   containing :class:`~django.forms.ModelChoiceIteratorValue` instances as the
    
  734.   first ``value`` element in each choice. In most cases this proxies
    
  735.   transparently, but if you need the ``field`` value itself, use the
    
  736.   :attr:`.ModelChoiceIteratorValue.value` attribute instead.
    
  737. 
    
  738. .. _deprecated-features-3.1:
    
  739. 
    
  740. Features deprecated in 3.1
    
  741. ==========================
    
  742. 
    
  743. .. _deprecated-jsonfield:
    
  744. 
    
  745. PostgreSQL ``JSONField``
    
  746. ------------------------
    
  747. 
    
  748. ``django.contrib.postgres.fields.JSONField`` and
    
  749. ``django.contrib.postgres.forms.JSONField`` are deprecated in favor of
    
  750. :class:`.models.JSONField` and
    
  751. :class:`forms.JSONField <django.forms.JSONField>`.
    
  752. 
    
  753. The undocumented ``django.contrib.postgres.fields.jsonb.KeyTransform`` and
    
  754. ``django.contrib.postgres.fields.jsonb.KeyTextTransform`` are also deprecated
    
  755. in favor of the transforms in ``django.db.models.fields.json``.
    
  756. 
    
  757. The new ``JSONField``\s, ``KeyTransform``, and ``KeyTextTransform`` can be used
    
  758. on all supported database backends.
    
  759. 
    
  760. Miscellaneous
    
  761. -------------
    
  762. 
    
  763. * ``PASSWORD_RESET_TIMEOUT_DAYS`` setting is deprecated in favor of
    
  764.   :setting:`PASSWORD_RESET_TIMEOUT`.
    
  765. 
    
  766. * The undocumented usage of the :lookup:`isnull` lookup with non-boolean values
    
  767.   as the right-hand side is deprecated, use ``True`` or ``False`` instead.
    
  768. 
    
  769. * The barely documented ``django.db.models.query_utils.InvalidQuery`` exception
    
  770.   class is deprecated in favor of
    
  771.   :class:`~django.core.exceptions.FieldDoesNotExist` and
    
  772.   :class:`~django.core.exceptions.FieldError`.
    
  773. 
    
  774. * The ``django-admin.py`` entry point is deprecated in favor of
    
  775.   ``django-admin``.
    
  776. 
    
  777. * The ``HttpRequest.is_ajax()`` method is deprecated as it relied on a
    
  778.   jQuery-specific way of signifying AJAX calls, while current usage tends to
    
  779.   use the JavaScript `Fetch API
    
  780.   <https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API>`_. Depending on
    
  781.   your use case, you can either write your own AJAX detection method, or use
    
  782.   the new :meth:`.HttpRequest.accepts` method if your code depends on the
    
  783.   client ``Accept`` HTTP header.
    
  784. 
    
  785.   If you are writing your own AJAX detection method, ``request.is_ajax()`` can
    
  786.   be reproduced exactly as
    
  787.   ``request.headers.get('x-requested-with') == 'XMLHttpRequest'``.
    
  788. 
    
  789. * Passing ``None`` as the first argument to
    
  790.   ``django.utils.deprecation.MiddlewareMixin.__init__()`` is deprecated.
    
  791. 
    
  792. * The encoding format of cookies values used by
    
  793.   :class:`~django.contrib.messages.storage.cookie.CookieStorage` is different
    
  794.   from the format generated by older versions of Django. Support for the old
    
  795.   format remains until Django 4.0.
    
  796. 
    
  797. * The encoding format of sessions is different from the format generated by
    
  798.   older versions of Django. Support for the old format remains until Django
    
  799.   4.0.
    
  800. 
    
  801. * The purely documentational ``providing_args`` argument for
    
  802.   :class:`~django.dispatch.Signal` is deprecated. If you rely on this
    
  803.   argument as documentation, you can move the text to a code comment or
    
  804.   docstring.
    
  805. 
    
  806. * Calling ``django.utils.crypto.get_random_string()`` without a ``length``
    
  807.   argument is deprecated.
    
  808. 
    
  809. * The ``list`` message for :class:`~django.forms.ModelMultipleChoiceField` is
    
  810.   deprecated in favor of ``invalid_list``.
    
  811. 
    
  812. * Passing raw column aliases to :meth:`.QuerySet.order_by` is deprecated. The
    
  813.   same result can be achieved by passing aliases in a
    
  814.   :class:`~django.db.models.expressions.RawSQL` instead beforehand.
    
  815. 
    
  816. * The ``NullBooleanField`` model field is deprecated in favor of
    
  817.   ``BooleanField(null=True)``.
    
  818. 
    
  819. * ``django.conf.urls.url()`` alias of :func:`django.urls.re_path` is
    
  820.   deprecated.
    
  821. 
    
  822. * The ``{% ifequal %}`` and ``{% ifnotequal %}`` template tags are deprecated
    
  823.   in favor of :ttag:`{% if %}<if>`. ``{% if %}`` covers all use cases, but if
    
  824.   you need to continue using these tags, they can be extracted from Django to a
    
  825.   module and included as a built-in tag in the :class:`'builtins'
    
  826.   <django.template.backends.django.DjangoTemplates>` option in
    
  827.   :setting:`OPTIONS <TEMPLATES-OPTIONS>`.
    
  828. 
    
  829. * ``DEFAULT_HASHING_ALGORITHM`` transitional setting is deprecated.
    
  830. 
    
  831. .. _removed-features-3.1:
    
  832. 
    
  833. Features removed in 3.1
    
  834. =======================
    
  835. 
    
  836. These features have reached the end of their deprecation cycle and are removed
    
  837. in Django 3.1.
    
  838. 
    
  839. See :ref:`deprecated-features-2.2` for details on these changes, including how
    
  840. to remove usage of these features.
    
  841. 
    
  842. * ``django.utils.timezone.FixedOffset`` is removed.
    
  843. 
    
  844. * ``django.core.paginator.QuerySetPaginator`` is removed.
    
  845. 
    
  846. * A model's ``Meta.ordering`` doesn't affect ``GROUP BY`` queries.
    
  847. 
    
  848. * ``django.contrib.postgres.fields.FloatRangeField`` and
    
  849.   ``django.contrib.postgres.forms.FloatRangeField`` are removed.
    
  850. 
    
  851. * The ``FILE_CHARSET`` setting is removed.
    
  852. 
    
  853. * ``django.contrib.staticfiles.storage.CachedStaticFilesStorage`` is removed.
    
  854. 
    
  855. * The ``RemoteUserBackend.configure_user()`` method requires ``request`` as the
    
  856.   first positional argument.
    
  857. 
    
  858. * Support for ``SimpleTestCase.allow_database_queries`` and
    
  859.   ``TransactionTestCase.multi_db`` is removed.