Jasconius Jasconius - 6 months ago 17
Python Question

Django ORM - assigning a raw value to DecimalField

EDIT!!! - The casting of the value to a string seems to work fine when I create a new object, but when I try to edit an existing object, it does not allow it.

So I have a decimal field in one of my models of Decimal(3,2)

When I query up all these objects and try to set this field:

fieldName = 0.85


OR

fieldName = .85


It will throw a hissy fit, "Cannot convert float to DecimalField, try converting to a string first"...

So then I do:

fieldName = str(0.85)


same error.

I even tried:

fieldName = "0.85"


Same error. Am I running into some sort of framework bug here, or what? Note that when I actually go into Django Admin and manually edit the objects, it works fine.

I am running Django 1.1 on Python 2.6

Answer
from decimal import Decimal
object.fieldName = Decimal("0.85")

or

f = 0.85
object.fieldName = Decimal(str(f))