GitHub

Chapter 31 · What changed

31.2 The second decade: from 1.9 to 5.0

Middleware rebuilt as an onion, Python 2 dropped, routes with path, ASGI and asynchronous views, indexes and constraints as objects, forms drawn by templates, zoneinfo: decisions of 1.9 to 5.0, and what each left at this commit.

Django 1.9 came on 1 December 2015 and 5.0 on 4 December 2023 (docs/releases/1.9.txt, docs/releases/5.0.txt; What changed). Several decisions took many releases. At this commit the old way often survives, as a shim, a name, or a class kept for old migrations. The years before are told in The first decade: from a newsroom to 1.8.

Middleware, rebuilt as an onion

1.10 rebuilt middleware as an onion, “to solve the lack of strict request/response layering”. Each middleware is wrapped round those listed after it. A response that a middleware returns early passes out only through the layers outside it. Before, a middleware was a class listed in MIDDLEWARE_CLASSES, whose hooks the handler called in turn. A response that one middleware’s process_request returned still passed through the process_response of every middleware, those that never saw the request among them.

DEP 5, written by Carl Meyer in 2016, replaced the request and response hooks with factories, listed in a new setting, MIDDLEWARE. Each is called once with the layer inside it, to make a callable from a request to a response. 2.0 removed the old setting. BaseHandler.load_middleware builds the nest once (BaseHandler.load_middleware; Building the middleware chain).

The old hooks live on in MiddlewareMixin, which DEP 5 offered so that one class could serve both settings. The DEP meant the mixin to “begin its own deprecation path” once the old setting was gone. The mixin never did. At this commit it is the base of all Django’s middleware but RemoteUserMiddleware and its subclass PersistentRemoteUserMiddleware. It lived in django.utils.deprecation, the module of the deprecation warnings, until 6.2 moved it to django.middleware.

Python 3 alone, and numbers that name the series

1.11, in 2017, was the last release to support Python 2. 2.0 supports Python 3.4 to 3.7. Its leading 2 marked no break of its own, but the start of “a loose form of semantic versioning”. 3.0 removed the private helpers kept for Python 2, the vendored six among them. 4.0 removed the aliases force_text and smart_text, leaving force_str and smart_str (docs/releases/3.0.txt, docs/releases/4.0.txt, force_str; Text and bytes: django/utils/encoding.py).

Routes without regular expressions

2.0 added routes, URL patterns written with django.urls.path, in which each parameter has a converter that both matches and converts it. Before, a URL pattern was a regular expression given to the function url of django.conf.urls, and what it captured reached the view as strings. DEP 201, by Tom Christie, Sjoerd Job Postmus and Aymeric Augustin, found the expressions “unnecessarily verbose and complex for the vast majority of use-cases”. It proposed a syntax after Flask’s. 2.0 made the old function available as django.urls.re_path, and kept url as an alias until 4.0.

The old function’s module, django/conf/urls/__init__.py, now holds include, which it imports from django.urls, and the default error views a resolver falls back on. As the DEP planned, a route is still expanded into a regular expression, as its RoutePattern is made (_route_to_regex; Routes, regular expressions and converters).

Indexes and constraints as objects

1.11 made an index an object, Index, which “can be subclassed to support different index types”. Before, a model asked for an index over several columns with the option index_together of its Meta, and for a rule over them with Options.unique_together alone. 2.2 made rules objects too, CheckConstraint and UniqueConstraint. 3.2 let an index be made on expressions, and 4.0 a unique constraint. 4.1 made model validation check the constraints (docs/releases/3.2.txt, docs/releases/4.0.txt, docs/releases/4.1.txt, Model.validate_constraints; Constraints and indexes).

4.2 deprecated index_together, and 5.1 removed it, but not from migrations. AlterIndexTogether remains, supported only for migration files from before 4.2, with “no plan to deprecate or remove it” (docs/releases/4.2.txt, docs/releases/5.1.txt, AlterIndexTogether; The model operations: CreateModel, DeleteModel, RenameModel and the option operations). Options.unique_together was never deprecated. The documentation recommends a UniqueConstraint, and says the option “may be deprecated in the future”.

Widgets and forms drawn by templates

Widgets wrote their HTML in Python until 1.11 moved their rendering to the template system “to ease customizing widgets”. 4.0 did the same for forms, formsets and error lists. 4.1 added a layout in <div> elements, which became the default in 5.0. RenderableFormMixin.as_div, as_p, as_table and as_ul each render a template (RenderableFormMixin; Rendering a form: renderers and templates).

Asynchronous Django

DEP 9, written by Andrew Godwin in 2019, proposed to make every part of Django that could block asynchronous, while keeping every synchronous interface. Each part was to pass through stages: first its synchronous code behind an asynchronous wrapper that runs it in a thread, later asynchronous code behind a synchronous wrapper. 3.0 began at the outermost layer: Django could be served as an ASGI application, though “internally everything remains synchronous”. Code marked as unsafe, such as the ORM’s, refused to run where an event loop was running. ASGIHandler is that entry, and async_unsafe the mark, which raises SynchronousOnlyOperation.

3.1 let views and middleware be asynchronous, under ASGI or WSGI. The handler puts an adapter wherever a layer and the one inside it differ in mode (docs/releases/3.1.txt, BaseHandler.adapt_method_mode; Sync and async in one chain). The DEP had meant the core to become asynchronous, with WSGI running it in an event loop. Instead the core has two forms, BaseHandler._get_response and BaseHandler._get_response_async. A WSGI handler builds a synchronous chain, in which an asynchronous view runs in a one-off event loop.

4.0 gave the cache asynchronous methods. 4.1 gave the queryset “an asynchronous interface for all data access operations”, though “the underlying database operations remain synchronous”. At this commit they still do: QuerySet.aget runs QuerySet.get through sync_to_async, “sync-native, with an async wrapper” in DEP 9’s words (QuerySet.aget; The asynchronous methods of a queryset). The proposal is still filed as accepted, not final.

Time zones from the standard library

1.11 made pytz a required dependency “to simplify Django’s timezone handling”. 3.2 accepted zones that were not pytz’s, such as those of zoneinfo, in the standard library from Python 3.9. 4.0 made zoneinfo the default and deprecated pytz. The transitional setting USE_DEPRECATED_PYTZ let a project keep pytz through 4.x. 5.0 removed both. get_default_timezone makes a zoneinfo.ZoneInfo of TIME_ZONE (get_default_timezone; Time zones).

PostgreSQL’s JSON for every database, and a second driver

1.9 added a JSON field to django.contrib.postgres. 3.1 put one in Django’s core for every supported database, with the lookups and transforms that had been PostgreSQL’s alone. The old field was deprecated with it and removed in 4.0, but a stub was to remain “for compatibility with historical migrations”. At this commit the stub, in django/contrib/postgres/fields/jsonb.py, is a subclass of the new field. Its Field.system_check_removed_details makes a model that uses it fail the checks. NullBooleanField, deprecated in 3.1, ended the same way (Field._check_deprecation_details; The model checks).

4.2 added psycopg 3 beside psycopg2, under the same "ENGINE". Its notes said psycopg2 was “likely to be deprecated and removed at some point in the future”. At this commit it is neither. django/db/backends/postgresql/psycopg_any.py imports psycopg where it can and psycopg2 otherwise, and sets is_psycopg3, which the backend asks where the two differ (PostgreSQL).

Applications that find their configuration

Since 3.2 Django finds an application’s configuration itself, if its apps.py defines a single AppConfig subclass. Before, a package could name the class in a variable, default_app_config, so that INSTALLED_APPS could list just the package. 3.2’s notes say the variable was introduced “with the intent to switch the ecosystem” to naming the class, “but the switch didn’t happen”. 3.2 deprecated it, and 4.1 removed it. AppConfig.create takes an entry first as a module, and then as the path of a class (AppConfig.create; Populating the app registry).

New projects ahead of the defaults

1.9 added password validation, off by default and on in the settings startproject writes. 3.2 likewise wrote BigAutoField into new projects, while DEFAULT_AUTO_FIELD kept AutoField. That default caught up in 6.0 (docs/releases/6.0.txt; What changed in models and fields). The default of USE_TZ, which startproject has set since 1.4, had caught up in 5.0. The validators have not: django/conf/global_settings.py leaves AUTH_PASSWORD_VALIDATORS empty, and django/conf/project_template/project_name/settings.py-tpl fills it (Password validation: validate_password and the validators).

Where 5.0 hands on

5.0’s own changes, and those of every release after it to this commit, are gathered by part of Django, and What changed sets out their larger movements.