Skip to content Skip to sidebar Skip to footer

Django Combine Models.decimalfield With Forms -> Error: Quantize Result Has Too Many Digits For Current Context

I want to combine a model decimal field with a forms choice field. The field in the model: sum = models.DecimalField(max_digits=2, decimal_places=2) The field in the form: sum = f

Solution 1:

It's just a guess, but I bet you need to put Decimals in there:

WORK_HOUR_CHOICES = (
    (Decimal("0"), '0'),
    (Decimal("0.5"), '0.5'),
    (Decimal("1"), '1'),
    (Decimal("1.5"), '1.5'),
    (Decimal("2"), '2'),
    (Decimal("2.5"), '2.5')
)

You can't initialize a Decimal with a float constant, you have to use a string.

>>>fromdecimal import Decimal>>>Decimal(1.5)
Traceback (most recent calllast):
  File "<stdin>", line 1, in<module>
  File "C:\software\Python25\lib\decimal.py", line 578, in __new__
    "First convert the float to a string")
TypeError: Cannot convertfloatto Decimal.  Firstconvert the floatto a string
>>>Decimal("1.5")
Decimal("1.5")

Post a Comment for "Django Combine Models.decimalfield With Forms -> Error: Quantize Result Has Too Many Digits For Current Context"