2.2.txt 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. ========================
  2. Django 2.2 release notes
  3. ========================
  4. *April 1, 2019*
  5. Welcome to Django 2.2!
  6. These release notes cover the :ref:`new features <whats-new-2.2>`, as well as
  7. some :ref:`backwards incompatible changes <backwards-incompatible-2.2>` you'll
  8. want to be aware of when upgrading from Django 2.1 or earlier. We've
  9. :ref:`begun the deprecation process for some features
  10. <deprecated-features-2.2>`.
  11. See the :doc:`/howto/upgrade-version` guide if you're updating an existing
  12. project.
  13. Django 2.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 1.11, will
  16. end in April 2020.
  17. Python compatibility
  18. ====================
  19. Django 2.2 supports Python 3.5, 3.6, 3.7, 3.8 (as of 2.2.8), and 3.9 (as of
  20. 2.2.17). We **highly recommend** and only officially support the latest release
  21. of each series.
  22. .. _whats-new-2.2:
  23. What's new in Django 2.2
  24. ========================
  25. Constraints
  26. -----------
  27. The new :class:`~django.db.models.CheckConstraint` and
  28. :class:`~django.db.models.UniqueConstraint` classes enable adding custom
  29. database constraints. Constraints are added to models using the
  30. :attr:`Meta.constraints <django.db.models.Options.constraints>` option.
  31. Minor features
  32. --------------
  33. :mod:`django.contrib.admin`
  34. ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  35. * Added a CSS class to the column headers of
  36. :class:`~django.contrib.admin.TabularInline`.
  37. :mod:`django.contrib.auth`
  38. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  39. * The ``HttpRequest`` is now passed as the first positional argument to
  40. :meth:`.RemoteUserBackend.configure_user`, if it accepts it.
  41. :mod:`django.contrib.gis`
  42. ~~~~~~~~~~~~~~~~~~~~~~~~~
  43. * Added Oracle support for the
  44. :class:`~django.contrib.gis.db.models.functions.Envelope` function.
  45. * Added SpatiaLite support for the :lookup:`coveredby` and :lookup:`covers`
  46. lookups.
  47. :mod:`django.contrib.postgres`
  48. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  49. * The new ``ordering`` argument for
  50. :class:`~django.contrib.postgres.aggregates.ArrayAgg` and
  51. :class:`~django.contrib.postgres.aggregates.StringAgg` determines the
  52. ordering of the aggregated elements.
  53. * The new :class:`~django.contrib.postgres.indexes.BTreeIndex`,
  54. :class:`~django.contrib.postgres.indexes.HashIndex` and
  55. :class:`~django.contrib.postgres.indexes.SpGistIndex` classes allow
  56. creating ``B-Tree``, ``hash``, and ``SP-GiST`` indexes in the database.
  57. * :class:`~django.contrib.postgres.indexes.BrinIndex` now has the
  58. ``autosummarize`` parameter.
  59. * The new ``search_type`` parameter of
  60. :class:`~django.contrib.postgres.search.SearchQuery` allows searching for
  61. a phrase or raw expression.
  62. :mod:`django.contrib.staticfiles`
  63. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  64. * Added path matching to the :option:`collectstatic --ignore` option so that
  65. patterns like ``/vendor/*.js`` can be used.
  66. Database backends
  67. ~~~~~~~~~~~~~~~~~
  68. * Added result streaming for :meth:`.QuerySet.iterator` on SQLite.
  69. Generic Views
  70. ~~~~~~~~~~~~~
  71. * The new :meth:`View.setup <django.views.generic.base.View.setup>` hook
  72. initializes view attributes before calling
  73. :meth:`~django.views.generic.base.View.dispatch`. It allows mixins to set up
  74. instance attributes for reuse in child classes.
  75. Internationalization
  76. ~~~~~~~~~~~~~~~~~~~~
  77. * Added support and translations for the Armenian language.
  78. Management Commands
  79. ~~~~~~~~~~~~~~~~~~~
  80. * The new :option:`--force-color` option forces colorization of the command
  81. output.
  82. * :djadmin:`inspectdb` now creates models for foreign tables on PostgreSQL.
  83. * :option:`inspectdb --include-views` now creates models for materialized views
  84. on Oracle and PostgreSQL.
  85. * The new :option:`inspectdb --include-partitions` option allows creating
  86. models for partition tables on PostgreSQL. In older versions, models are
  87. created child tables instead the parent.
  88. * :djadmin:`inspectdb` now introspects :class:`~django.db.models.DurationField`
  89. for Oracle and PostgreSQL, and :class:`~django.db.models.AutoField` for
  90. SQLite.
  91. * On Oracle, :djadmin:`dbshell` is wrapped with ``rlwrap``, if available.
  92. ``rlwrap`` provides a command history and editing of keyboard input.
  93. * The new :option:`makemigrations --no-header` option avoids writing header
  94. comments in generated migration file(s). This option is also available for
  95. :djadmin:`squashmigrations`.
  96. * :djadmin:`runserver` can now use `Watchman
  97. <https://facebook.github.io/watchman/>`_ to improve the performance of
  98. watching a large number of files for changes.
  99. Migrations
  100. ~~~~~~~~~~
  101. * The new :option:`migrate --plan` option prints the list of migration
  102. operations that will be performed.
  103. * ``NoneType`` can now be serialized in migrations.
  104. * You can now :ref:`register custom serializers <custom-migration-serializers>`
  105. for migrations.
  106. Models
  107. ~~~~~~
  108. * Added support for PostgreSQL operator classes (:attr:`.Index.opclasses`).
  109. * Added support for partial indexes (:attr:`.Index.condition`).
  110. * Added the :class:`~django.db.models.functions.NullIf` and
  111. :class:`~django.db.models.functions.Reverse` database functions, as well as
  112. many :ref:`math database functions <math-functions>`.
  113. * Setting the new ``ignore_conflicts`` parameter of
  114. :meth:`.QuerySet.bulk_create` to ``True`` tells the database to ignore
  115. failure to insert rows that fail uniqueness constraints or other checks.
  116. * The new :class:`~django.db.models.functions.ExtractIsoYear` function extracts
  117. ISO-8601 week-numbering years from :class:`~django.db.models.DateField` and
  118. :class:`~django.db.models.DateTimeField`, and the new :lookup:`iso_year`
  119. lookup allows querying by an ISO-8601 week-numbering year.
  120. * The new :meth:`.QuerySet.bulk_update` method allows efficiently updating
  121. specific fields on multiple model instances.
  122. * Django no longer always starts a transaction when a single query is being
  123. performed, such as ``Model.save()``, ``QuerySet.update()``, and
  124. ``Model.delete()``. This improves the performance of autocommit by reducing
  125. the number of database round trips.
  126. * Added SQLite support for the :class:`~django.db.models.StdDev` and
  127. :class:`~django.db.models.Variance` functions.
  128. * The handling of ``DISTINCT`` aggregation is added to the
  129. :class:`~django.db.models.Aggregate` class. Adding :attr:`allow_distinct =
  130. True <django.db.models.Aggregate.allow_distinct>` as a class attribute on
  131. ``Aggregate`` subclasses allows a ``distinct`` keyword argument to be
  132. specified on initialization to ensure that the aggregate function is only
  133. called for each distinct value of ``expressions``.
  134. * The :meth:`.RelatedManager.add`, :meth:`~.RelatedManager.create`,
  135. :meth:`~.RelatedManager.remove`, :meth:`~.RelatedManager.set`,
  136. ``get_or_create()``, and ``update_or_create()`` methods are now allowed on
  137. many-to-many relationships with intermediate models. The new
  138. ``through_defaults`` argument is used to specify values for new intermediate
  139. model instance(s).
  140. Requests and Responses
  141. ~~~~~~~~~~~~~~~~~~~~~~
  142. * Added :attr:`.HttpRequest.headers` to allow simple access to a request's
  143. headers.
  144. Serialization
  145. ~~~~~~~~~~~~~
  146. * You can now deserialize data using natural keys containing :ref:`forward
  147. references <natural-keys-and-forward-references>` by passing
  148. ``handle_forward_references=True`` to ``serializers.deserialize()``.
  149. Additionally, :djadmin:`loaddata` handles forward references automatically.
  150. Tests
  151. ~~~~~
  152. * The new :meth:`.SimpleTestCase.assertURLEqual` assertion checks for a given
  153. URL, ignoring the ordering of the query string.
  154. :meth:`~.SimpleTestCase.assertRedirects` uses the new assertion.
  155. * The test :class:`~.django.test.Client` now supports automatic JSON
  156. serialization of list and tuple ``data`` when
  157. ``content_type='application/json'``.
  158. * The new :setting:`ORACLE_MANAGED_FILES <TEST_ORACLE_MANAGED_FILES>` test
  159. database setting allows using Oracle Managed Files (OMF) tablespaces.
  160. * Deferrable database constraints are now checked at the end of each
  161. :class:`~django.test.TestCase` test on SQLite 3.20+, just like on other
  162. backends that support deferrable constraints. These checks aren't implemented
  163. for older versions of SQLite because they would require expensive table
  164. introspection there.
  165. * :class:`~django.test.runner.DiscoverRunner` now skips the setup of databases
  166. not :ref:`referenced by tests<testing-multi-db>`.
  167. URLs
  168. ~~~~
  169. * The new :attr:`.ResolverMatch.route` attribute stores the route of the
  170. matching URL pattern.
  171. Validators
  172. ~~~~~~~~~~
  173. * :class:`.MaxValueValidator`, :class:`.MinValueValidator`,
  174. :class:`.MinLengthValidator`, and :class:`.MaxLengthValidator` now accept
  175. a callable ``limit_value``.
  176. .. _backwards-incompatible-2.2:
  177. Backwards incompatible changes in 2.2
  178. =====================================
  179. Database backend API
  180. --------------------
  181. This section describes changes that may be needed in third-party database
  182. backends.
  183. * Third-party database backends must implement support for table check
  184. constraints or set ``DatabaseFeatures.supports_table_check_constraints`` to
  185. ``False``.
  186. * Third party database backends must implement support for ignoring
  187. constraints or uniqueness errors while inserting or set
  188. ``DatabaseFeatures.supports_ignore_conflicts`` to ``False``.
  189. * Third party database backends must implement introspection for
  190. ``DurationField`` or set ``DatabaseFeatures.can_introspect_duration_field``
  191. to ``False``.
  192. * ``DatabaseFeatures.uses_savepoints`` now defaults to ``True``.
  193. * Third party database backends must implement support for partial indexes or
  194. set ``DatabaseFeatures.supports_partial_indexes`` to ``False``.
  195. * ``DatabaseIntrospection.table_name_converter()`` and
  196. ``column_name_converter()`` are removed. Third party database backends may
  197. need to instead implement ``DatabaseIntrospection.identifier_converter()``.
  198. In that case, the constraint names that
  199. ``DatabaseIntrospection.get_constraints()`` returns must be normalized by
  200. ``identifier_converter()``.
  201. * SQL generation for indexes is moved from :class:`~django.db.models.Index` to
  202. ``SchemaEditor`` and these ``SchemaEditor`` methods are added:
  203. * ``_create_primary_key_sql()`` and ``_delete_primary_key_sql()``
  204. * ``_delete_index_sql()`` (to pair with ``_create_index_sql()``)
  205. * ``_delete_unique_sql`` (to pair with ``_create_unique_sql()``)
  206. * ``_delete_fk_sql()`` (to pair with ``_create_fk_sql()``)
  207. * ``_create_check_sql()`` and ``_delete_check_sql()``
  208. * The third argument of ``DatabaseWrapper.__init__()``,
  209. ``allow_thread_sharing``, is removed.
  210. Admin actions are no longer collected from base ``ModelAdmin`` classes
  211. ----------------------------------------------------------------------
  212. For example, in older versions of Django::
  213. from django.contrib import admin
  214. class BaseAdmin(admin.ModelAdmin):
  215. actions = ["a"]
  216. class SubAdmin(BaseAdmin):
  217. actions = ["b"]
  218. ``SubAdmin`` would have actions ``'a'`` and ``'b'``.
  219. Now ``actions`` follows standard Python inheritance. To get the same result as
  220. before::
  221. class SubAdmin(BaseAdmin):
  222. actions = BaseAdmin.actions + ["b"]
  223. :mod:`django.contrib.gis`
  224. -------------------------
  225. * Support for GDAL 1.9 and 1.10 is dropped.
  226. ``TransactionTestCase`` serialized data loading
  227. -----------------------------------------------
  228. Initial data migrations are now loaded in
  229. :class:`~django.test.TransactionTestCase` at the end of the test, after the
  230. database flush. In older versions, this data was loaded at the beginning of the
  231. test, but this prevents the :option:`test --keepdb` option from working
  232. properly (the database was empty at the end of the whole test suite). This
  233. change shouldn't have an impact on your tests unless you've customized
  234. :class:`~django.test.TransactionTestCase`'s internals.
  235. ``sqlparse`` is required dependency
  236. -----------------------------------
  237. To simplify a few parts of Django's database handling, :pypi:`sqlparse 0.2.2+
  238. <sqlparse>` is now a required dependency. It's automatically installed along
  239. with Django.
  240. ``cached_property`` aliases
  241. ---------------------------
  242. In usage like::
  243. from django.utils.functional import cached_property
  244. class A:
  245. @cached_property
  246. def base(self):
  247. return ...
  248. alias = base
  249. ``alias`` is not cached. Where the problem can be detected (Python 3.6 and
  250. later), such usage now raises ``TypeError: Cannot assign the same
  251. cached_property to two different names ('base' and 'alias').``
  252. Use this instead::
  253. import operator
  254. class A:
  255. ...
  256. alias = property(operator.attrgetter("base"))
  257. Permissions for proxy models
  258. ----------------------------
  259. :ref:`Permissions for proxy models <proxy-models-permissions-topic>` are now
  260. created using the content type of the proxy model rather than the content type
  261. of the concrete model. A migration will update existing permissions when you
  262. run :djadmin:`migrate`.
  263. In the admin, the change is transparent for proxy models having the same
  264. ``app_label`` as their concrete model. However, in older versions, users with
  265. permissions for a proxy model with a *different* ``app_label`` than its
  266. concrete model couldn't access the model in the admin. That's now fixed, but
  267. you might want to audit the permissions assignments for such proxy models
  268. (``[add|view|change|delete]_myproxy``) prior to upgrading to ensure the new
  269. access is appropriate.
  270. Finally, proxy model permission strings must be updated to use their own
  271. ``app_label``. For example, for ``app.MyProxyModel`` inheriting from
  272. ``other_app.ConcreteModel``, update
  273. ``user.has_perm('other_app.add_myproxymodel')`` to
  274. ``user.has_perm('app.add_myproxymodel')``.
  275. Merging of form ``Media`` assets
  276. --------------------------------
  277. Form ``Media`` assets are now merged using a topological sort algorithm, as the
  278. old pairwise merging algorithm is insufficient for some cases. CSS and
  279. JavaScript files which don't include their dependencies may now be sorted
  280. incorrectly (where the old algorithm produced results correctly by
  281. coincidence).
  282. Audit all ``Media`` classes for any missing dependencies. For example,
  283. widgets depending on ``django.jQuery`` must specify
  284. ``js=['admin/js/jquery.init.js', ...]`` when :ref:`declaring form media assets
  285. <assets-as-a-static-definition>`.
  286. Miscellaneous
  287. -------------
  288. * To improve readability, the ``UUIDField`` form field now displays values with
  289. dashes, e.g. ``550e8400-e29b-41d4-a716-446655440000`` instead of
  290. ``550e8400e29b41d4a716446655440000``.
  291. * On SQLite, ``PositiveIntegerField`` and ``PositiveSmallIntegerField`` now
  292. include a check constraint to prevent negative values in the database. If you
  293. have existing invalid data and run a migration that recreates a table, you'll
  294. see ``CHECK constraint failed``.
  295. * For consistency with WSGI servers, the test client now sets the
  296. ``Content-Length`` header to a string rather than an integer.
  297. * The return value of :func:`django.utils.text.slugify` is no longer marked as
  298. HTML safe.
  299. * The default truncation character used by the :tfilter:`urlizetrunc`,
  300. :tfilter:`truncatechars`, :tfilter:`truncatechars_html`,
  301. :tfilter:`truncatewords`, and :tfilter:`truncatewords_html` template filters
  302. is now the real ellipsis character (``…``) instead of 3 dots. You may have to
  303. adapt some test output comparisons.
  304. * Support for bytestring paths in the template filesystem loader is removed.
  305. * :func:`django.utils.http.urlsafe_base64_encode` now returns a string instead
  306. of a bytestring, and :func:`django.utils.http.urlsafe_base64_decode` may no
  307. longer be passed a bytestring.
  308. * Support for ``cx_Oracle`` < 6.0 is removed.
  309. * The minimum supported version of ``mysqlclient`` is increased from 1.3.7 to
  310. 1.3.13.
  311. * The minimum supported version of SQLite is increased from 3.7.15 to 3.8.3.
  312. * In an attempt to provide more semantic query data, ``NullBooleanSelect`` now
  313. renders ``<option>`` values of ``unknown``, ``true``, and ``false`` instead
  314. of ``1``, ``2``, and ``3``. For backwards compatibility, the old values are
  315. still accepted as data.
  316. * :attr:`Group.name <django.contrib.auth.models.Group.name>` ``max_length``
  317. is increased from 80 to 150 characters.
  318. * Tests that violate deferrable database constraints now error when run on
  319. SQLite 3.20+, just like on other backends that support such constraints.
  320. * To catch usage mistakes, the test :class:`~django.test.Client` and
  321. :func:`django.utils.http.urlencode` now raise ``TypeError`` if ``None`` is
  322. passed as a value to encode because ``None`` can't be encoded in GET and POST
  323. data. Either pass an empty string or omit the value.
  324. * The ``ping_google`` management command now defaults to ``https``
  325. instead of ``http`` for the sitemap's URL. If your site uses http, use the
  326. new ``ping_google --sitemap-uses-http`` option. If you use the
  327. ``django.contrib.sitemaps.ping_google`` function, set the new
  328. ``sitemap_uses_https`` argument to ``False``.
  329. * :djadmin:`runserver` no longer supports ``pyinotify`` (replaced by Watchman).
  330. * The :class:`~django.db.models.Avg`, :class:`~django.db.models.StdDev`, and
  331. :class:`~django.db.models.Variance` aggregate functions now return a
  332. ``Decimal`` instead of a ``float`` when the input is ``Decimal``.
  333. * Tests will fail on SQLite if apps without migrations have relations to apps
  334. with migrations. This has been a documented restriction since migrations were
  335. added in Django 1.7, but it fails more reliably now. You'll see tests failing
  336. with errors like ``no such table: <app_label>_<model>``. This was observed
  337. with several third-party apps that had models in tests without migrations.
  338. You must add migrations for such models.
  339. * Providing an integer in the ``key`` argument of the :meth:`.cache.delete` or
  340. :meth:`.cache.get` now raises :exc:`ValueError`.
  341. * Plural equations for some languages are changed, because the latest versions
  342. from Transifex are incorporated.
  343. .. note::
  344. The ability to handle ``.po`` files containing different plural equations
  345. for the same language was added in Django 2.2.12.
  346. .. _deprecated-features-2.2:
  347. Features deprecated in 2.2
  348. ==========================
  349. Model ``Meta.ordering`` will no longer affect ``GROUP BY`` queries
  350. ------------------------------------------------------------------
  351. A model's ``Meta.ordering`` affecting ``GROUP BY`` queries (such as
  352. ``.annotate().values()``) is a common source of confusion. Such queries now
  353. issue a deprecation warning with the advice to add an ``order_by()`` to retain
  354. the current query. ``Meta.ordering`` will be ignored in such queries starting
  355. in Django 3.1.
  356. Miscellaneous
  357. -------------
  358. * ``django.utils.timezone.FixedOffset`` is deprecated in favor of
  359. :class:`datetime.timezone`.
  360. * The undocumented ``QuerySetPaginator`` alias of
  361. ``django.core.paginator.Paginator`` is deprecated.
  362. * The ``FloatRangeField`` model and form fields in ``django.contrib.postgres``
  363. are deprecated in favor of a new name, ``DecimalRangeField``, to match the
  364. underlying ``numrange`` data type used in the database.
  365. * The ``FILE_CHARSET`` setting is deprecated. Starting with Django 3.1, files
  366. read from disk must be UTF-8 encoded.
  367. * ``django.contrib.staticfiles.storage.CachedStaticFilesStorage`` is
  368. deprecated due to the intractable problems that it has. Use
  369. :class:`.ManifestStaticFilesStorage` or a third-party cloud storage instead.
  370. * :meth:`.RemoteUserBackend.configure_user` is now passed ``request`` as the
  371. first positional argument, if it accepts it. Support for overrides that don't
  372. accept it will be removed in Django 3.1.
  373. * The ``SimpleTestCase.allow_database_queries``,
  374. ``TransactionTestCase.multi_db``, and ``TestCase.multi_db``
  375. attributes are deprecated in favor of :attr:`.SimpleTestCase.databases`,
  376. :attr:`.TransactionTestCase.databases`, and :attr:`.TestCase.databases`.
  377. These new attributes allow databases dependencies to be declared in order to
  378. prevent unexpected queries against non-default databases to leak state
  379. between tests. The previous behavior of ``allow_database_queries=True`` and
  380. ``multi_db=True`` can be achieved by setting ``databases='__all__'``.