News/Tech News

Async handlers are added to Django 4.1

Published on Nov 26, 2022

In addition, the latest version of the popular Python web framework provides an asynchronous interface for all data access operations.

In Django 4.1, a major update to the Python-based web framework, asynchronous handlers and an ORM interface are added, but some changes are backward-incompatible as well.

As of August 3, Django 4.1 can be accessed from the Django project website. According to the release notes, the new release introduces asynchronous handlers for class-based views; view subclasses can now define async HTTP method handlers.

In order to support asynchronous object-relational mapping, QuerySet now offers an asynchronous interface for all data access operations. The new interface allows developers to write asynchronous code without having to write ORM operations in sync_to_async(). The underlying database operations remain synchronous at this stage.

Python 3.8, Python 3.9, and Python 3.10 are supported by Django 4.1, but only the latest versions of each series. Django 4.1 also includes:

Model validation now checks check, unique, and exclusion constraints defined in the Meta.constraints option.

A new set of forms that use div elements are available to assist users who use screen readers and other assistive technologies.

The CSRF_COOKIE_MASKED transitional setting allows developers to specify whether the CSRF (Cross Site Request Forgery) cookie should be masked.

SECRET_KEY_FALLBACKS provides a list of values for secret key validation.

For templates, the HTML –script– element’s id attribute is no longer required when wrapping the json_script template.

The DiscoverRunner now supports parallel testing on Windows, macOS, and any other operating system where spawn is the default multiprocessing start method.

There were a number of backward-incompatible changes made, including some that affected the API used by the database back-end. The release notes describe specific changes such as BaseDatabaseFeatures.has_case_insensitive_like being changed from True to False to reflect the behavior of most databases.

The upstream support for PostgreSQL 10 will end in November. PostgreSQL 11 and higher is supported by Django 4.1. In addition, MariaDB 10.2 support ended in May, so Django 4.1 supports MariaDB 10.3 and higher.

Further, in order to unify the behavior with many-to-many relationships for unsaved model instances, a reverse foreign key will now raise a ValueError when calling related managers.