0

An AssertionError occurred while trying to operate asynchronous view as class-based view in django(CBV)

The python code i wrote is as follows.

urls.py

from django.contrib import admin
from django.conf.urls import include
from django.urls import re_path
from rest_framework import routers
from blog import views

router = routers.DefaultRouter()

router.register(r'test', views.TestView, 'hello')

urlpatterns = [
    re_path(r'^admin/', admin.site.urls),
    re_path(r'', include(router.urls)),
    re_path(r'async_view/', include(router.urls))
]

views.py

class TestView(viewsets.ViewSet):
    LOG = logging.getLogger('test')
    async def list(self, request, *args, **kwargs):
        self.LOG.info("async method start!!")
        queryset = Post.objects.none
        await asyncio.sleep(3)
        return HttpResponse("Hello async world!")

asgi.py

import os

from django.core.asgi import get_asgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings')

application = get_asgi_application()

When i execute command below

uvicorn mysite.asgi:application --limit-concurrency 12 --reload --host 0.0.0.0 --port 80

then, An AssertionError occurred

AssertionError: Expected a `Response`, `HttpResponse` or `HttpStreamingResponse` to be returned from the view, but received a `<class 'coroutine'>`

Which part is wrong??

1 Answer 1

0

It seems Django Rest Framework does not support async views as of now.

How to use async function based views in DRF?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.