Update after comments

This commit is contained in:
enrico 2022-09-01 14:19:46 +08:00
parent 472ca5b716
commit 7b18380321
2 changed files with 6 additions and 5 deletions

View File

@ -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:

View File

@ -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__