如何在 FastAPI mongo 的模式中使用十进制?

问题描述 投票:0回答:0

我有带有 mongodb 的 python fastapi 应用程序。 下面我提供了创建文档的架构和代码。 我需要使用十进制,因为浮点数据类型中的浮点存在问题。但是,在 mongo 中使用 condecimal 会出现问题。

class FlowerTypeSchema(BaseModel):
    id: UUID = None
    merchant_id: Optional[UUID] = None
    name: LangSchema
    description: LangSchema
    photo: Optional[str] = None
    custom: Optional[bool] = False
    rating: Optional[condecimal(ge=0)] = 0
    tags: List[str]
    consumables: List[ProductsSpentSchema]
    created_at: datetime = None
    updated_at: datetime = None
    deleted_at: datetime = None
async def create_flower_type(flower_type: FlowerTypeSchema) -> FlowerTypeResponse:
    flower_type.id = uuid4()
    flower_type.photo = None
    flower_type.created_at = datetime.now(pytz.timezone(TIMEZONE)).strftime("%Y-%m-%dT%H:%M:%S")
    flower_type.updated_at = datetime.now(pytz.timezone(TIMEZONE)).strftime("%Y-%m-%dT%H:%M:%S")
    await flower_types_collection.insert_one(flower_type.dict())
    return FlowerTypeResponse(**flower_type.dict())

我想使用小数进行评级(以及稍后的价格),但我收到错误:

2024-06-01 14:46:32,147 - uvicorn.error - ERROR - Exception in ASGI application
Traceback (most recent call last):
  File "C:\Users\Akmal\AppData\Local\Programs\Python\Python310\lib\site-packages\uvicorn\protocols\http\h11_impl.py", line 408, in run_asgi
    result = await app(  # type: ignore[func-returns-value]
  File "C:\Users\Akmal\AppData\Local\Programs\Python\Python310\lib\site-packages\uvicorn\middleware\proxy_headers.py", line 84, in __call__
    return await self.app(scope, receive, send)
  File "C:\Users\Akmal\AppData\Local\Programs\Python\Python310\lib\site-packages\fastapi\applications.py", line 1106, in __call__
    await super().__call__(scope, receive, send)
  File "C:\Users\Akmal\AppData\Local\Programs\Python\Python310\lib\site-packages\starlette\applications.py", line 122, in __call__
    await self.middleware_stack(scope, receive, send)
  File "C:\Users\Akmal\AppData\Local\Programs\Python\Python310\lib\site-packages\starlette\middleware\errors.py", line 184, in __call__
    raise exc
  File "C:\Users\Akmal\AppData\Local\Programs\Python\Python310\lib\site-packages\starlette\middleware\errors.py", line 162, in __call__
    await self.app(scope, receive, _send)
  File "C:\Users\Akmal\AppData\Local\Programs\Python\Python310\lib\site-packages\starlette\middleware\cors.py", line 83, in __call__
    await self.app(scope, receive, send)
  File "C:\Users\Akmal\AppData\Local\Programs\Python\Python310\lib\site-packages\starlette\middleware\exceptions.py", line 79, in __call__
    raise exc
  File "C:\Users\Akmal\AppData\Local\Programs\Python\Python310\lib\site-packages\starlette\middleware\exceptions.py", line 68, in __call__
    await self.app(scope, receive, sender)
  File "C:\Users\Akmal\AppData\Local\Programs\Python\Python310\lib\site-packages\fastapi\middleware\asyncexitstack.py", line 20, in __call__
    raise e
  File "C:\Users\Akmal\AppData\Local\Programs\Python\Python310\lib\site-packages\fastapi\middleware\asyncexitstack.py", line 17, in __call__
    await self.app(scope, receive, send)
  File "C:\Users\Akmal\AppData\Local\Programs\Python\Python310\lib\site-packages\starlette\routing.py", line 718, in __call__
    await route.handle(scope, receive, send)
  File "C:\Users\Akmal\AppData\Local\Programs\Python\Python310\lib\site-packages\starlette\routing.py", line 276, in handle
    await self.app(scope, receive, send)
  File "C:\Users\Akmal\AppData\Local\Programs\Python\Python310\lib\site-packages\starlette\routing.py", line 66, in app
    response = await func(request)
  File "C:\Users\Akmal\AppData\Local\Programs\Python\Python310\lib\site-packages\fastapi\routing.py", line 274, in app
    raw_response = await run_endpoint_function(
  File "C:\Users\Akmal\AppData\Local\Programs\Python\Python310\lib\site-packages\fastapi\routing.py", line 191, in run_endpoint_function
    return await dependant.call(**values)
  File "c:\Users\Akmal\IT\OyGul\og-py-merchant-content-service\routes\flower_types.py", line 31, in create_flower_type_data
    new_flower_type = await create_flower_type(flower_type=data)
  File "c:\Users\Akmal\IT\OyGul\og-py-merchant-content-service\database\flower_types.py", line 17, in create_flower_type
    await flower_types_collection.insert_one(flower_type.dict())
  File "C:\Users\Akmal\AppData\Local\Programs\Python\Python310\lib\concurrent\futures\thread.py", line 58, in run
    result = self.fn(*self.args, **self.kwargs)
  File "C:\Users\Akmal\AppData\Local\Programs\Python\Python310\lib\site-packages\pymongo\collection.py", line 669, in insert_one
    self._insert_one(
  File "C:\Users\Akmal\AppData\Local\Programs\Python\Python310\lib\site-packages\pymongo\collection.py", line 609, in _insert_one
    self.__database.client._retryable_write(acknowledged, _insert_command, session)
  File "C:\Users\Akmal\AppData\Local\Programs\Python\Python310\lib\site-packages\pymongo\mongo_client.py", line 1523, in _retryable_write
    return self._retry_with_session(retryable, func, s, bulk)
  File "C:\Users\Akmal\AppData\Local\Programs\Python\Python310\lib\site-packages\pymongo\mongo_client.py", line 1421, in _retry_with_session
    return self._retry_internal(
  File "C:\Users\Akmal\AppData\Local\Programs\Python\Python310\lib\site-packages\pymongo\_csot.py", line 107, in csot_wrapper
    return func(self, *args, **kwargs)
  File "C:\Users\Akmal\AppData\Local\Programs\Python\Python310\lib\site-packages\pymongo\mongo_client.py", line 1462, in _retry_internal
    ).run()
  File "C:\Users\Akmal\AppData\Local\Programs\Python\Python310\lib\site-packages\pymongo\mongo_client.py", line 2315, in run
    return self._read() if self._is_read else self._write()
  File "C:\Users\Akmal\AppData\Local\Programs\Python\Python310\lib\site-packages\pymongo\mongo_client.py", line 2422, in _write
    return self._func(self._session, conn, self._retryable)  # type: ignore
  File "C:\Users\Akmal\AppData\Local\Programs\Python\Python310\lib\site-packages\pymongo\collection.py", line 597, in _insert_command
    result = conn.command(
  File "C:\Users\Akmal\AppData\Local\Programs\Python\Python310\lib\site-packages\pymongo\helpers.py", line 322, in inner
    return func(*args, **kwargs)
  File "C:\Users\Akmal\AppData\Local\Programs\Python\Python310\lib\site-packages\pymongo\pool.py", line 996, in command
    self._raise_connection_failure(error)
  File "C:\Users\Akmal\AppData\Local\Programs\Python\Python310\lib\site-packages\pymongo\pool.py", line 968, in command
    return command(
  File "C:\Users\Akmal\AppData\Local\Programs\Python\Python310\lib\site-packages\pymongo\network.py", line 151, in command
    request_id, msg, size, max_doc_size = message._op_msg(
  File "C:\Users\Akmal\AppData\Local\Programs\Python\Python310\lib\site-packages\pymongo\message.py", line 762, in _op_msg
    return _op_msg_uncompressed(flags, command, identifier, docs, opts)
bson.errors.InvalidDocument: cannot encode object: Decimal('4.5'), of type: <class 'decimal.Decimal'>
python mongodb decimal fastapi
© www.soinside.com 2019 - 2024. All rights reserved.