[PR 8617 - Async implementation] Async exception when returning Response(Serializer(instance).data)
· Issue #8657 · encode/django-rest-framework (original) (raw)
SynchronousOnlyOperation at /api/create_user You cannot call this from an async context - use a thread or sync_to_async.
Request Method: POST Request URL: http://0.0.0.0:8000/api/create_user Django Version: 4.1.1 Python Executable: /usr/local/bin/python3.10 Python Version: 3.10.7 Python Path: ['.', '/usr/local/bin', '/code', '/code/bb_backend', '/usr/local/lib/python310.zip', '/usr/local/lib/python3.10', '/usr/local/lib/python3.10/lib-dynload', '/root/.local/lib/python3.10/site-packages', '/usr/local/lib/python3.10/site-packages'] Server time: Sun, 18 Sep 2022 06:16:30 +0000 Installed Applications: ['django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'rest_framework', '', 'corsheaders'] Installed Middleware: ['corsheaders.middleware.CorsMiddleware', 'django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback (most recent call last): File "/usr/local/lib/python3.10/site-packages/asgiref/sync.py", line 472, in thread_handler raise exc_info[1] File "/usr/local/lib/python3.10/site-packages/django/core/handlers/exception.py", line 42, in inner response = await get_response(request) File "/usr/local/lib/python3.10/site-packages/django/core/handlers/base.py", line 253, in _get_response_async response = await wrapped_callback( File "/usr/local/lib/python3.10/site-packages/rest_framework/views.py", line 539, in async_dispatch response = self.handle_exception(exc) File "/usr/local/lib/python3.10/site-packages/rest_framework/views.py", line 469, in handle_exception self.raise_uncaught_exception(exc) File "/usr/local/lib/python3.10/site-packages/rest_framework/views.py", line 480, in raise_uncaught_exception raise exc File "/usr/local/lib/python3.10/site-packages/rest_framework/views.py", line 536, in async_dispatch response = await handler(request, *args, **kwargs) File "/usr/local/lib/python3.10/site-packages/rest_framework/decorators.py", line 54, in async_handler return await func(*args, **kwargs) File "/code/bb_backend/./bb_core/api/auth.py", line 55, in create_user return Response(UserSerializer(instance=user).data) File "/usr/local/lib/python3.10/site-packages/rest_framework/serializers.py", line 555, in data ret = super().data File "/usr/local/lib/python3.10/site-packages/rest_framework/serializers.py", line 253, in data self._data = self.to_representation(self.instance) File "/usr/local/lib/python3.10/site-packages/rest_framework/serializers.py", line 522, in to_representation ret[field.field_name] = field.to_representation(attribute) File "/usr/local/lib/python3.10/site-packages/rest_framework/relations.py", line 565, in to_representation return [ File "/usr/local/lib/python3.10/site-packages/django/db/models/query.py", line 394, in iter self._fetch_all() File "/usr/local/lib/python3.10/site-packages/django/db/models/query.py", line 1866, in _fetch_all self._result_cache = list(self._iterable_class(self)) File "/usr/local/lib/python3.10/site-packages/django/db/models/query.py", line 87, in iter results = compiler.execute_sql( File "/usr/local/lib/python3.10/site-packages/django/db/models/sql/compiler.py", line 1393, in execute_sql cursor = self.connection.cursor() File "/usr/local/lib/python3.10/site-packages/django/utils/asyncio.py", line 24, in inner raise SynchronousOnlyOperation(message)
Exception Type: SynchronousOnlyOperation at /api/create_user Exception Value: You cannot call this from an async context - use a thread or sync_to_async.