Skip to content

Commit e838e0c

Browse files
committed
Support async view methods in extend_schema_view.
1 parent 01ec586 commit e838e0c

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

drf_spectacular/drainage.py

+14-5
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,12 @@ def get_view_method_names(view, schema=None) -> List[str]:
182182
return [
183183
item for item in dir(view) if callable(getattr(view, item)) and (
184184
item in view.http_method_names
185-
or item in schema.method_mapping.values()
186-
or item == 'list'
185+
or (
186+
item in schema.async_method_mapping.values()
187+
if view.view_is_async
188+
else item in schema.method_mapping.values()
189+
)
190+
or item == ('alist' if view.view_is_async else 'list')
187191
or hasattr(getattr(view, item), 'mapping')
188192
)
189193
]
@@ -202,9 +206,14 @@ def isolate_view_method(view, method_name):
202206
if method_name in view.__dict__ and method.__name__ != 'handler':
203207
return method
204208

205-
@functools.wraps(method)
206-
def wrapped_method(self, request, *args, **kwargs):
207-
return method(self, request, *args, **kwargs)
209+
if getattr(view, "view_is_async", False):
210+
@functools.wraps(method)
211+
async def wrapped_method(self, request, *args, **kwargs):
212+
return await method(self, request, *args, **kwargs)
213+
else:
214+
@functools.wraps(method)
215+
def wrapped_method(self, request, *args, **kwargs):
216+
return method(self, request, *args, **kwargs)
208217

209218
# wraps() will only create a shallow copy of method.__dict__. Updates to "kwargs"
210219
# via @extend_schema would leak to the original method. Isolate by creating a copy.

drf_spectacular/openapi.py

+7
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,13 @@ class AutoSchema(ViewInspector):
5757
'patch': 'partial_update',
5858
'delete': 'destroy',
5959
}
60+
async_method_mapping = {
61+
'get': 'aretrieve',
62+
'post': 'acreate',
63+
'put': 'aupdate',
64+
'patch': 'partial_aupdate',
65+
'delete': 'adestroy',
66+
}
6067

6168
def get_operation(
6269
self,

0 commit comments

Comments
 (0)