You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When I tested my API to get the country list I got a following error when pydantic tried to serialize a info field asynchronously:
Traceback
Traceback (most recent call last): File "/home/branya/Документи/My Projects/Django/guide_self_api/.venv/lib/python3.11/site-packages/asgiref/sync.py", line 254, in __call__ return call_result.result() ^^^^^^^^^^^^^^^^^^^^ File "/usr/lib/python3.11/concurrent/futures/_base.py", line 449, in result return self.__get_result() ^^^^^^^^^^^^^^^^^^^ File "/usr/lib/python3.11/concurrent/futures/_base.py", line 401, in __get_result raise self._exception File "/home/branya/Документи/My Projects/Django/guide_self_api/.venv/lib/python3.11/site-packages/asgiref/sync.py", line 331, in main_wrap result = await self.awaitable(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/branya/Документи/My Projects/Django/guide_self_api/src/travels/tests/test_api.py", line 43, in test_api_returns_country_list_data response = await self.client.get('/countries', headers={'Accept-Language': 'en'}) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/branya/Документи/My Projects/Django/guide_self_api/.venv/lib/python3.11/site-packages/ninja/testing/client.py", line 169, in _call return NinjaResponse(await func(request, **kwargs)) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/branya/Документи/My Projects/Django/guide_self_api/.venv/lib/python3.11/site-packages/ninja/operation.py", line 473, in _async_view return await cast(AsyncOperation, operation).run(request, *a, **kw) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/branya/Документи/My Projects/Django/guide_self_api/.venv/lib/python3.11/site-packages/ninja/operation.py", line 336, in run return self.api.on_exception(request, e) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/branya/Документи/My Projects/Django/guide_self_api/.venv/lib/python3.11/site-packages/ninja/main.py", line 515, in on_exception return handler(request, exc) ^^^^^^^^^^^^^^^^^^^^^ File "/home/branya/Документи/My Projects/Django/guide_self_api/.venv/lib/python3.11/site-packages/ninja/errors.py", line 114, in _default_exception raise exc # let django deal with it ^^^^^^^^^ File "/home/branya/Документи/My Projects/Django/guide_self_api/.venv/lib/python3.11/site-packages/ninja/operation.py", line 334, in run return self._result_to_response(request, result, temporal_response) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/branya/Документи/My Projects/Django/guide_self_api/.venv/lib/python3.11/site-packages/ninja/operation.py", line 260, in _result_to_response validated_object = response_model.model_validate( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/branya/Документи/My Projects/Django/guide_self_api/.venv/lib/python3.11/site-packages/pydantic/main.py", line 568, in model_validate return cls.__pydantic_validator__.validate_python( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^pydantic_core._pydantic_core.ValidationError: 6 validation errors for NinjaResponseSchemaresponse.0.info Error extracting attribute: SynchronousOnlyOperation: You cannot call this from an async context - use a thread or sync_to_async. [type=get_attribute_error, input_value=<DjangoGetter: <Country: england>>, input_type=DjangoGetter] For further information visit https://errors.pydantic.dev/2.8/v/get_attribute_error
I tried to use async\await in the resolve_info method, but pydantic cannot work asynchronously. I'm beginner in the asynchronous, so I could miss or don't know some thing to solve this issue.
Please help me. Thank you for your help earlier
Possible solution
I'm not sure this is good solution, because the data is serialized twice.
When I tested my API to get the country list I got a following
error
whenpydantic
tried to serialize ainfo
field asynchronously:Traceback
Code
I tried to use
async\await
in theresolve_info
method, butpydantic
cannot work asynchronously. I'm beginner in the asynchronous, so I could miss or don't know some thing to solve this issue.Please help me. Thank you for your help earlier
Possible solution
I'm not sure this is good solution, because the data is serialized twice.
The text was updated successfully, but these errors were encountered: