From 0400834a33790ae8fefff170df30031992ec19d7 Mon Sep 17 00:00:00 2001 From: enrico Date: Thu, 1 Sep 2022 09:02:00 +0800 Subject: [PATCH] Added documentation --- docs/api-guide/views.md | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/docs/api-guide/views.md b/docs/api-guide/views.md index 878a291b2..3a93c2042 100644 --- a/docs/api-guide/views.md +++ b/docs/api-guide/views.md @@ -217,6 +217,22 @@ You may pass `None` in order to exclude the view from schema generation. def view(request): return Response({"message": "Will not appear in schema!"}) +# Async Views + +When using Django 4.1 and above, REST framework allows you to work with async class based and function based views. + +For class based view, the view needs the handler methods such as `.get()`, `.post()`, `put()`, `patch()` and `.delete()` to be all async otherwise Django will raise an exception. For function based view the view needs to be async. + +For example: + + class ListUsers(APIView): + async def get(self, request): + return Response("message": "This is an async class based view."}) + + + @api_view(['GET']) + async def view(request): + return Response({"message": "This is an async function based view."}) [cite]: https://reinout.vanrees.org/weblog/2011/08/24/class-based-views-usage.html [cite2]: http://www.boredomandlaziness.org/2012/05/djangos-cbvs-are-not-mistake-but.html @@ -224,4 +240,3 @@ You may pass `None` in order to exclude the view from schema generation. [throttling]: throttling.md [schemas]: schemas.md [classy-drf]: http://www.cdrf.co -