Есть у меня дб, получать данные через список не удобно, хотел преобразовать в обьекты, но с толкнулся с проблемой: Ответ result : (1, 'Test', 'rtest', "{'test': 0}", 5555.0, 'true') Код (1, 'Test', 'rtest', "{'test': 0}", 5555.0, 'true') Класс (модель): class TariffStructure( *, id: int, name: str | None, description: str | None, host_data_json: str | None, price: float | None, is_active: bool | None ) Python class TariffStructure( *, id: int, name: str | None, description: str | None, host_data_json: str | None, price: float | None, is_active: bool | None ) Ошибка: Traceback (most recent call last): File "/usr/lib/python3/dist-packages/uvicorn/protocols/http/httptools_impl.py", line 399, in run_asgi result = await app( # type: ignore[func-returns-value] ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/lib/python3/dist-packages/uvicorn/middleware/proxy_headers.py", line 70, in __call__ return await self.app(scope, receive, send) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/lib/python3/dist-packages/fastapi/applications.py", line 1054, in __call__ await super().__call__(scope, receive, send) File "/home/reques6e/.local/lib/python3.11/site-packages/starlette/applications.py", line 123, in __call__ await self.middleware_stack(scope, receive, send) File "/home/reques6e/.local/lib/python3.11/site-packages/starlette/middleware/errors.py", line 186, in __call__ raise exc File "/home/reques6e/.local/lib/python3.11/site-packages/starlette/middleware/errors.py", line 164, in __call__ await self.app(scope, receive, _send) File "/home/reques6e/.local/lib/python3.11/site-packages/starlette/middleware/exceptions.py", line 62, in __call__ await wrap_app_handling_exceptions(self.app, conn)(scope, receive, send) File "/home/reques6e/.local/lib/python3.11/site-packages/starlette/_exception_handler.py", line 64, in wrapped_app raise exc File "/home/reques6e/.local/lib/python3.11/site-packages/starlette/_exception_handler.py", line 53, in wrapped_app await app(scope, receive, sender) File "/home/reques6e/.local/lib/python3.11/site-packages/starlette/routing.py", line 758, in __call__ await self.middleware_stack(scope, receive, send) File "/home/reques6e/.local/lib/python3.11/site-packages/starlette/routing.py", line 778, in app await route.handle(scope, receive, send) File "/home/reques6e/.local/lib/python3.11/site-packages/starlette/routing.py", line 299, in handle await self.app(scope, receive, send) File "/home/reques6e/.local/lib/python3.11/site-packages/starlette/routing.py", line 79, in app await wrap_app_handling_exceptions(app, request)(scope, receive, send) File "/home/reques6e/.local/lib/python3.11/site-packages/starlette/_exception_handler.py", line 64, in wrapped_app raise exc File "/home/reques6e/.local/lib/python3.11/site-packages/starlette/_exception_handler.py", line 53, in wrapped_app await app(scope, receive, sender) File "/home/reques6e/.local/lib/python3.11/site-packages/starlette/routing.py", line 74, in app response = await func(request) ^^^^^^^^^^^^^^^^^^^ File "/usr/lib/python3/dist-packages/fastapi/routing.py", line 278, in app raw_response = await run_endpoint_function( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/lib/python3/dist-packages/fastapi/routing.py", line 191, in run_endpoint_function return await dependant.call(**values) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/reques6e/Documents/GitHub/TurBotAPI/api/tariff/router.py", line 27, in get_tariff if tariff := await db.Tariff.get( ^^^^^^^^^^^^^^^^^^^^ File "/home/reques6e/Documents/GitHub/TurBotAPI/api/db.py", line 50, in get return TariffStructure(**result) ^^^^^^^^^^^^^^^^^^^^^^^^^ TypeError: api.tariff.models.TariffStructure() argument after ** must be a mapping, not tuple Код Traceback (most recent call last): File "/usr/lib/python3/dist-packages/uvicorn/protocols/http/httptools_impl.py", line 399, in run_asgi result = await app( # type: ignore[func-returns-value] ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/lib/python3/dist-packages/uvicorn/middleware/proxy_headers.py", line 70, in __call__ return await self.app(scope, receive, send) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/lib/python3/dist-packages/fastapi/applications.py", line 1054, in __call__ await super().__call__(scope, receive, send) File "/home/reques6e/.local/lib/python3.11/site-packages/starlette/applications.py", line 123, in __call__ await self.middleware_stack(scope, receive, send) File "/home/reques6e/.local/lib/python3.11/site-packages/starlette/middleware/errors.py", line 186, in __call__ raise exc File "/home/reques6e/.local/lib/python3.11/site-packages/starlette/middleware/errors.py", line 164, in __call__ await self.app(scope, receive, _send) File "/home/reques6e/.local/lib/python3.11/site-packages/starlette/middleware/exceptions.py", line 62, in __call__ await wrap_app_handling_exceptions(self.app, conn)(scope, receive, send) File "/home/reques6e/.local/lib/python3.11/site-packages/starlette/_exception_handler.py", line 64, in wrapped_app raise exc File "/home/reques6e/.local/lib/python3.11/site-packages/starlette/_exception_handler.py", line 53, in wrapped_app await app(scope, receive, sender) File "/home/reques6e/.local/lib/python3.11/site-packages/starlette/routing.py", line 758, in __call__ await self.middleware_stack(scope, receive, send) File "/home/reques6e/.local/lib/python3.11/site-packages/starlette/routing.py", line 778, in app await route.handle(scope, receive, send) File "/home/reques6e/.local/lib/python3.11/site-packages/starlette/routing.py", line 299, in handle await self.app(scope, receive, send) File "/home/reques6e/.local/lib/python3.11/site-packages/starlette/routing.py", line 79, in app await wrap_app_handling_exceptions(app, request)(scope, receive, send) File "/home/reques6e/.local/lib/python3.11/site-packages/starlette/_exception_handler.py", line 64, in wrapped_app raise exc File "/home/reques6e/.local/lib/python3.11/site-packages/starlette/_exception_handler.py", line 53, in wrapped_app await app(scope, receive, sender) File "/home/reques6e/.local/lib/python3.11/site-packages/starlette/routing.py", line 74, in app response = await func(request) ^^^^^^^^^^^^^^^^^^^ File "/usr/lib/python3/dist-packages/fastapi/routing.py", line 278, in app raw_response = await run_endpoint_function( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/lib/python3/dist-packages/fastapi/routing.py", line 191, in run_endpoint_function return await dependant.call(**values) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/reques6e/Documents/GitHub/TurBotAPI/api/tariff/router.py", line 27, in get_tariff if tariff := await db.Tariff.get( ^^^^^^^^^^^^^^^^^^^^ File "/home/reques6e/Documents/GitHub/TurBotAPI/api/db.py", line 50, in get return TariffStructure(**result) ^^^^^^^^^^^^^^^^^^^^^^^^^ TypeError: api.tariff.models.TariffStructure() argument after ** must be a mapping, not tuple Код: class TariffHandlers: def __init__( self, hosting_instance ) -> None: self.database = hosting_instance async def get( self, id: int ): """Возвращает tariff по ID.""" async with aiosqlite.connect(self.database.db_name) as db: async with db.execute('SELECT * FROM api_tariff WHERE id = ?', (id,)) as cursor: result = await cursor.fetchone() return TariffStructure(**result) Python class TariffHandlers: def __init__( self, hosting_instance ) -> None: self.database = hosting_instance async def get( self, id: int ): """Возвращает tariff по ID.""" async with aiosqlite.connect(self.database.db_name) as db: async with db.execute('SELECT * FROM api_tariff WHERE id = ?', (id,)) as cursor: result = await cursor.fetchone() return TariffStructure(**result) ДБ: CREATE TABLE IF NOT EXISTS api_tariff ( id INTEGER PRIMARY KEY AUTOINCREMENT, -- ID Тарифа name TEXT, -- Название тарифа description TEXT, -- Описание Тарифа host_data_json TEXT, -- Хост Дата (сервер и прочие настройки) price REAL, -- Цена is_active BOOLEAN -- Вкл/Выкл ); SQL CREATE TABLE IF NOT EXISTS api_tariff ( id INTEGER PRIMARY KEY AUTOINCREMENT, -- ID Тарифа name TEXT, -- Название тарифа description TEXT, -- Описание Тарифа host_data_json TEXT, -- Хост Дата (сервер и прочие настройки) price REAL, -- Цена is_active BOOLEAN -- Вкл/Выкл ); Как решить эту проблему?
async with aiosqlite.connect(self.database.db_name) as db: db.row_factory = aiosqlite.Row # тут юзаешь row_factory async with db.execute('SELECT * FROM api_tariff WHERE id = ?', (id,)) as cursor: result = await cursor.fetchone() if result: return TariffStructure(**result) Python async with aiosqlite.connect(self.database.db_name) as db: db.row_factory = aiosqlite.Row # тут юзаешь row_factory async with db.execute('SELECT * FROM api_tariff WHERE id = ?', (id,)) as cursor: result = await cursor.fetchone() if result: return TariffStructure(**result) тут ты будешь результат в виде словаря получать --- Сообщение объединено с предыдущим 9 ноя 2024 Или создай словарь вручную async with aiosqlite.connect(self.database.db_name) as db: async with db.execute('SELECT * FROM api_tariff WHERE id = ?', (id,)) as cursor: result = await cursor.fetchone() if result: columns = [column[0] for column in cursor.description] result_dict = dict(zip(columns, result)) return TariffStructure(**result_dict) Python async with aiosqlite.connect(self.database.db_name) as db: async with db.execute('SELECT * FROM api_tariff WHERE id = ?', (id,)) as cursor: result = await cursor.fetchone() if result: columns = [column[0] for column in cursor.description] result_dict = dict(zip(columns, result)) return TariffStructure(**result_dict) --- Сообщение объединено с предыдущим 9 ноя 2024 проблема в том что ты result через ** распаковать хочешь