What is new in Django 4.1?
Django 4.1 just released yesterday (August 3, 2022) and we are going to review the newest main features and changes.
See the How to upgrade Django to a newer version guide if you’re updating an existing project.
Python compatibility
Django 4.1 supports Python 3.8, 3.9, and 3.10. Highly recommended and only officially support the latest release of each series.
What’s new in Django 4.1
Asynchronous handlers for class-based views
View subclasses may now define async HTTP method handlers:
import asyncio
from django.http import HttpResponse
from django.views import View
class AsyncView(View):
async def get(self, request, *args, **kwargs):
# Perform view logic using await.
await asyncio.sleep(1)
return HttpResponse("Hello async world!")
See Asynchronous class-based views for more details.
Asynchronous ORM interface
QuerySet
now provides an asynchronous interface for all data access operations. These are named as-per the existing synchronous operations but with an a
prefix, for example acreate()
, aget()
, and so on.
The new interface allows you to write asynchronous code without needing to wrap ORM operations in sync_to_async()
:
async for author in Author.objects.filter(name__startswith="A"):
book = await author.books.afirst()
Note that, at this stage, the underlying database operations remain synchronous, with contributions ongoing to push asynchronous support down into the SQL compiler, and integrate asynchronous database drivers. The new asynchronous queryset interface currently encapsulates the necessary sync_to_async()
operations for you, and will allow your code to take advantage of developments in the ORM’s asynchronous support as it evolves.
See Asynchronous queries for details and limitations.
CSRF_COOKIE_MASKED
setting
The new CSRF_COOKIE_MASKED
transitional setting allows specifying whether to mask the CSRF cookie.
CsrfViewMiddleware
no longer masks the CSRF cookie like it does the CSRF token in the DOM. If you are upgrading multiple instances of the same project to Django 4.1, you should set CSRF_COOKIE_MASKED
to True
during the transition, in order to allow compatibility with the older versions of Django. Once the transition to 4.1 is complete you can stop overriding CSRF_COOKIE_MASKED
.
This setting is deprecated as of this release and will be removed in Django 5.0.
Validation of Constraints
Check
, unique
, and exclusion
constraints defined in the Meta.constraints
option are now checked during model validation.
Form rendering accessibility
In order to aid users with screen readers, and other assistive technology, new <div>
based form templates are available from this release. These provide more accessible navigation than the older templates, and are able to correctly group related controls, such as radio-lists, into fieldsets.
The new templates are recommended, and will become the default form rendering style when outputting a form, like {{ form }}
in a template, from Django 5.0.
In order to ease adopting the new output style, the default form and formset templates are now configurable at the project level via the FORM_RENDERER
setting.
See the Forms section (below) for full details.
Happy Coding!