Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

'ASGIRequest' object has no attribute '_request' #157

Open
tomiduss opened this issue Oct 1, 2024 · 0 comments
Open

'ASGIRequest' object has no attribute '_request' #157

tomiduss opened this issue Oct 1, 2024 · 0 comments

Comments

@tomiduss
Copy link

tomiduss commented Oct 1, 2024

I'm getting this error using the EventsViewSet, I installed with the drf package.

Versions:
django_eventstream==5.3.1
daphne==4.1.2
django==5.1.1
djangorestframework==3.15.2
This is the trace:

AttributeError: 'ASGIRequest' object has no attribute '_request'
[01/Oct/2024 19:19:19,879] ERROR:Internal Server Error: /api/v1/chat/events/test/
Traceback (most recent call last):
  File "/Users/tomasdussaillant/.local/share/virtualenvs/aorta-api-tCbWVDVZ/lib/python3.10/site-packages/asgiref/sync.py", line 518, in thread_handler
    raise exc_info[1]
  File "/Users/tomasdussaillant/.local/share/virtualenvs/aorta-api-tCbWVDVZ/lib/python3.10/site-packages/django/core/handlers/exception.py", line 42, in inner
    response = await get_response(request)
  File "/Users/tomasdussaillant/.local/share/virtualenvs/aorta-api-tCbWVDVZ/lib/python3.10/site-packages/django/core/handlers/base.py", line 253, in _get_response_async
    response = await wrapped_callback(
  File "/Users/tomasdussaillant/.local/share/virtualenvs/aorta-api-tCbWVDVZ/lib/python3.10/site-packages/asgiref/sync.py", line 468, in __call__
    ret = await asyncio.shield(exec_coro)
  File "/Users/tomasdussaillant/.local/share/virtualenvs/aorta-api-tCbWVDVZ/lib/python3.10/site-packages/asgiref/current_thread_executor.py", line 40, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/Users/tomasdussaillant/.local/share/virtualenvs/aorta-api-tCbWVDVZ/lib/python3.10/site-packages/asgiref/sync.py", line 522, in thread_handler
    return func(*args, **kwargs)
  File "/Users/tomasdussaillant/.local/share/virtualenvs/aorta-api-tCbWVDVZ/lib/python3.10/site-packages/django/views/decorators/csrf.py", line 65, in _view_wrapper
    return view_func(request, *args, **kwargs)
  File "/Users/tomasdussaillant/.local/share/virtualenvs/aorta-api-tCbWVDVZ/lib/python3.10/site-packages/rest_framework/viewsets.py", line 124, in view
    return self.dispatch(request, *args, **kwargs)
  File "/Users/tomasdussaillant/.local/share/virtualenvs/aorta-api-tCbWVDVZ/lib/python3.10/site-packages/rest_framework/views.py", line 509, in dispatch
    response = self.handle_exception(exc)
  File "/Users/tomasdussaillant/.local/share/virtualenvs/aorta-api-tCbWVDVZ/lib/python3.10/site-packages/rest_framework/views.py", line 469, in handle_exception
    self.raise_uncaught_exception(exc)
  File "/Users/tomasdussaillant/.local/share/virtualenvs/aorta-api-tCbWVDVZ/lib/python3.10/site-packages/rest_framework/views.py", line 480, in raise_uncaught_exception
    raise exc
  File "/Users/tomasdussaillant/.local/share/virtualenvs/aorta-api-tCbWVDVZ/lib/python3.10/site-packages/rest_framework/views.py", line 506, in dispatch
    response = handler(request, *args, **kwargs)
  File "/Users/tomasdussaillant/.local/share/virtualenvs/aorta-api-tCbWVDVZ/lib/python3.10/site-packages/django_eventstream/viewsets.py", line 110, in channel
    return self._stream_or_respond([channel], request._request)
  File "/Users/tomasdussaillant/.local/share/virtualenvs/aorta-api-tCbWVDVZ/lib/python3.10/site-packages/django_eventstream/viewsets.py", line 118, in _stream_or_respond
    request = django_request._request
AttributeError: 'ASGIRequest' object has no attribute '_request'

#settings.py
INSTALLED_APPS = [
    "daphne",
    ...,
    "django_eventstream",
    ...
]

ASGI_APPLICATION = "core.asgi.application"

redis_host, redis_port = CELERY_BROKER_URL.split("redis://")[1].split(":")

EVENTSTREAM_REDIS = {
    'host': redis_host,
    'port': redis_port,
    'db': 0,
}

This my view:

#views.py
class EventsView(EventsViewSet):
    permission_classes = [IsAdminOrIsResearch]
    channels = ["test"]

#urls.py
from django.urls import path, include
from django_eventstream import views
from rest_framework.routers import DefaultRouter

from .views import EventsView

router = DefaultRouter()
router.register(r'events', EventsView, basename='events')

urlpatterns = [
    path("", include(router.urls)),
]

However if I change line 118 in viewsets.py to:
request = django_request._request if hasattr(django_request, "_request") else django_request
it works.

I also noticed that if I don't set
ASGI_APPLICATION = "core.asgi.application"
Daphne won't let me run, which i feel should be mentioned in the README.

Regular views are working fine, but using the DRF support is useful because i can leverage permission_classes of drf views and integrates seamlessly with my auth logic.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant