Django 4.1 Released!
On August 3, 2022, Django 4.1 was just published, and in this article, we’ll go over the top five key improvements and new features.
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 Viewclass 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.
2. 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.
3. Requests and Responses¶
HttpResponse.set_cookie()
now supportstimedelta
objects for themax_age
argument.
4. django.contrib.gis
¶
- The new
GEOSGeometry.make_valid()
method allows converting invalid geometries to valid ones. - The new
clone
argument forGEOSGeometry.normalize()
allows creating a normalized clone of the geometry.
5. django.contrib.postgres
¶
- The new
BitXor()
aggregate function returns anint
of the bitwiseXOR
of all non-null input values. SpGistIndex
now supports covering indexes on PostgreSQL 14+.ExclusionConstraint
now supports covering exclusion constraints using SP-GiST indexes on PostgreSQL 14+.- The new
default_bounds
attribute ofDateTimeRangeField
andDecimalRangeField
allows specifying bounds for list and tuple inputs. ExclusionConstraint
now allows specifying operator classes with theOpClass()
expression.
More information can be found here.
Happy Coding!