diff --git a/docs/api-guide/views.md b/docs/api-guide/views.md index 14e13d41a..9e34191f3 100644 --- a/docs/api-guide/views.md +++ b/docs/api-guide/views.md @@ -219,9 +219,9 @@ You may pass `None` in order to exclude the view from schema generation. # Async Views -When using Django 4.1 and above, REST framework allows you to work with async class based and function based views. +When using Django 4.1 and above, REST framework allows you to work with async class 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 class based views, all handler methods must be async, otherwise Django will raise an exception. For function based views, the function itself must be async. For example: diff --git a/rest_framework/decorators.py b/rest_framework/decorators.py index 06d42ec09..9c526959b 100644 --- a/rest_framework/decorators.py +++ b/rest_framework/decorators.py @@ -55,10 +55,11 @@ def api_view(http_method_names=None): view_is_async = asyncio.iscoroutinefunction(func) - for method in http_method_names: - if view_is_async: + if view_is_async: + for method in http_method_names: setattr(WrappedAPIView, method.lower(), async_handler) - else: + else: + for method in http_method_names: setattr(WrappedAPIView, method.lower(), sync_handler) WrappedAPIView.__name__ = func.__name__