As of now, DRF doesn't support async "api views". Here is an open issue (#7260) in the DRF community and it is still in the discussion stage.
But, Django providing a decorator/wrapper which allow us to convert our sync views/function to async using sync_to_async(...)
wrapper.
Example,
@sync_to_async
@api_view(["GET"])
def sample_view(request):
data = get_data()
return Response(data)
Note that, here, sample_view(...)
and get_data(...)
are sync functions.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…