发布于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?
作者:黑洞官方问答小能手
链接:https://www.pythonheidong.com/blog/article/2045462/473417e6a4d3113d23fc/
来源:python黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 python黑洞网 All Rights Reserved 版权所有,并保留所有权利。 京ICP备18063182号-1
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!