mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-07-27 00:19:53 +03:00
Update after comments
This commit is contained in:
parent
472ca5b716
commit
7b18380321
|
@ -219,9 +219,9 @@ You may pass `None` in order to exclude the view from schema generation.
|
||||||
|
|
||||||
# Async Views
|
# 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:
|
For example:
|
||||||
|
|
||||||
|
|
|
@ -55,10 +55,11 @@ def api_view(http_method_names=None):
|
||||||
|
|
||||||
view_is_async = asyncio.iscoroutinefunction(func)
|
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)
|
setattr(WrappedAPIView, method.lower(), async_handler)
|
||||||
else:
|
else:
|
||||||
|
for method in http_method_names:
|
||||||
setattr(WrappedAPIView, method.lower(), sync_handler)
|
setattr(WrappedAPIView, method.lower(), sync_handler)
|
||||||
|
|
||||||
WrappedAPIView.__name__ = func.__name__
|
WrappedAPIView.__name__ = func.__name__
|
||||||
|
|
Loading…
Reference in New Issue
Block a user