程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长

+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

暂无数据

decimal.InvalidOperation in Tortoise ORM

发布于2024-11-23 21:35     阅读(409)     评论(0)     点赞(17)     收藏(3)


Here's how my model is defined:

class User(Model):
    id = BigIntField(primary_key=True, source_field="user_id")
    text = CharField(max_length=255, null=True)
    decimal = DecimalField(max_digits=27, decimal_places=27)

Let's consider this example where I create a user and pass him a random decimal number:

async def test():
    user = await User.create(decimal=1234567890.3)
    ...

This throws this error:

(.venv) n1qro@n1qro-Latitude-5480:~/Рабочий стол/TortoiseORM$ "/home/n1qro/Рабочий стол/TortoiseORM/.venv/bin/python" "/home/n1qro/Рабочий стол/TortoiseORM/main.py"
Traceback (most recent call last):
  File "/home/n1qro/Рабочий стол/TortoiseORM/main.py", line 19, in <module>
    asyncio.run(main())
  File "/usr/local/lib/python3.10/asyncio/runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "/usr/local/lib/python3.10/asyncio/base_events.py", line 649, in run_until_complete
    return future.result()
  File "/home/n1qro/Рабочий стол/TortoiseORM/main.py", line 15, in main
    await test()
  File "/home/n1qro/Рабочий стол/TortoiseORM/main.py", line 7, in test
    user = await User.create(decimal=1234567890.3)
  File "/home/n1qro/Рабочий стол/TortoiseORM/.venv/lib/python3.10/site-packages/tortoise/models.py", line 1140, in create
    instance = cls(**kwargs)
  File "/home/n1qro/Рабочий стол/TortoiseORM/.venv/lib/python3.10/site-packages/tortoise/models.py", line 660, in __init__
    for key in meta.fields.difference(self._set_kwargs(kwargs)):
  File "/home/n1qro/Рабочий стол/TortoiseORM/.venv/lib/python3.10/site-packages/tortoise/models.py", line 696, in _set_kwargs
    setattr(self, key, field_object.to_python_value(value))
  File "/home/n1qro/Рабочий стол/TortoiseORM/.venv/lib/python3.10/site-packages/tortoise/fields/data.py", line 288, in to_python_value
    value = Decimal(value).quantize(self.quant).normalize()
decimal.InvalidOperation: [<class 'decimal.InvalidOperation'>]

I suppose Tortoise ORM uses Decimal library under the hood to manage this field. I know nothing about this library. Tortoise ORM docs state that I just need to specify the number of decimal places and max digits for it to work. Can someone explain what am I doing wrong?docs_screenshot


解决方案


暂无回答



所属网站分类: 技术文章 > 问答

作者:黑洞官方问答小能手

链接:https://www.pythonheidong.com/blog/article/2045462/473417e6a4d3113d23fc/

来源:python黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

17 0
收藏该文
已收藏

评论内容:(最多支持255个字符)