0

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

2
  • Is there more to the error message than that? Do you know what value is? Looking at the docs, (search "InvalidOperation"), you can get that error if value is out of range. Commented Aug 30, 2024 at 19:14
  • Error is on the first line of test function. The value I am passing is a constant 1234567890.3. It should be in range because I specified max_digits=27. Here's the full error if you need it: pastebin.com/NSqug9vx Commented Aug 30, 2024 at 20:58

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.