[Answer]-Django Integer and Decimal field multiplication

1πŸ‘

I’m guessing int(instance.qty) is 2, and instance.sale_price is '180.00'β€” a string, not a numeric value, which, when multiplied by two, just duplicates the string:

>>> decimal.Decimal(2 * '180.00')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Frameworks/EPD64.framework/Versions/7.2/lib/python2.7/decimal.py", line 548, in __new__
    "Invalid literal for Decimal: %r" % value)
  File "/Library/Frameworks/EPD64.framework/Versions/7.2/lib/python2.7/decimal.py", line 3844, in _raise_error
    raise error(explanation)
decimal.InvalidOperation: Invalid literal for Decimal: '180.00180.00'

How is sale_price being set? I would hope that Django casts it as a Decimal, but is it possible it’s being stored as a string somehow?

πŸ‘€Karmel

Leave a comment