Async Methods in Class-Based Views
Leverage async CBV methods for non-blocking I/O.
# models.py
from django.db import models
class AsyncThing(models.Model):
name = models.CharField(max_length=80)
# admin.py
from django.contrib import admin
from .models import AsyncThing
@admin.register(AsyncThing)
class AsyncThingAdmin(admin.ModelAdmin):
list_display = ("name",)
# views.py
import httpx
from django.views import View
from django.http import JsonResponse
class AsyncStatus(View):
async def get(self, request):
async with httpx.AsyncClient() as client:
r = await client.get("https://httpbin.org/status/200")
return JsonResponse({"ok": r.status_code == 200})
Explanation:
-
Django supports
async defhandlers on CBVs. - Stick to async-safe libraries in async methods.
- Category Views (FBV & CBV)
- Total Views 382
- Last Modified 01 December, 2025
- Tags #async #cbv #views #performance
Previous snippet
Async Function-Based Views
Next snippet