views:

295

answers:

2

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

+1  A: 

The Django DecimalField is "...represented in by a python Decimal instance." You might try:

>>> obj.fieldName = Decimal("0.85")

Behavior may also vary depending on the database backend you are using. With sqlite, I am able to assign string values to DecimalFields in new and existing objects without error.

tcarobruce
Just saw this. Seems that form inputs put into a model instance with the sqlite backend will stay as unicode strings which stops you from doing any maths on them.
KayEss
+1  A: 
from decimal import Decimal
object.fieldName = Decimal("0.85")

or

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