Decimal to Decimal nightmare

179 views
Skip to first unread message

Newt

unread,
Apr 24, 2009, 10:37:10 AM4/24/09
to Django users
Hello,

in my latest application I have models with like 15 Decimal fields,
with different decimal places.

when I try to save an object of that model, I get sometimes (often,
but not always) this error:

File "/home/newt/django/1.0/sites/cdms/item/views.py", line 1054, in
item_copy
object.save()

File "/home/newt/django/1.0/lib/django/db/models/base.py", line 311,
in save

File "/home/newt/django/1.0/lib/django/db/models/base.py", line 371,
in save_base

File "/home/newt/django/1.0/lib/django/db/models/fields/
__init__.py", line 192, in get_db_prep_save

File "/home/newt/django/1.0/lib/django/db/models/fields/
__init__.py", line 609, in get_db_prep_value

File "/home/newt/django/1.0/lib/django/db/models/fields/
__init__.py", line 583, in to_python

File "/usr/lib/python2.5/decimal.py", line 650, in __new__
raise TypeError("Cannot convert %r to Decimal" % value)

TypeError: Cannot convert Decimal("0.0000") to Decimal

It fails on the first decimal field (at it's only one with 4 decimal
places) and it just happens sometimes.
I remember something like that used to happen with psycopg2 some year
ago.

I think it breaks when it tries to get the data ready for the SQL
statement.

In this project I have to use django-pyodbc connector to MSSQL
database.

I have tried both 1.02 and 1.1b branches of the connector with same
errorous results.

It's the most frustrating bug in django so far, as I'm completely
helpless and even google's silent about this.

Please does anyone know what's causing this, or how to get rid of
it...

thanks,

Ales

soniiic

unread,
Apr 24, 2009, 2:45:08 PM4/24/09
to Django users
Are you sure you're using it correctly? i googled it and came up with
this which is the exact same position you're in.

This never actually applies a value to be converted but it still tries
and that's what causes the error (i think)

>>> import decimal
>>> d = decimal.Decimal()
>>> reload(decimal)
<module 'decimal' from '/usr/lib/python2.5/decimal.pyc'>
>>> decimal.Decimal(d)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.5/decimal.py", line 617, in __new__
raise TypeError("Cannot convert %r to Decimal" % value)
TypeError: Cannot convert Decimal("0") to Decimal

http://mail.python.org/pipermail/python-list/2008-May/665477.html

Newt

unread,
Apr 25, 2009, 6:13:50 AM4/25/09
to Django users
Hi,
I'm pretty sure I use it correctly, but don't understand how this
fixes the problem :(.

I looked up the reload in the documentation and all it does a module
reload - where would I place it?
I was thinking i could convert all decimals to strings prior the
saving the new object, but it would not fix updates.

Ales

Graham Dumpleton

unread,
Apr 25, 2009, 6:40:29 AM4/25/09
to Django users
The psycopg2 problem was because the C extension module which is a
part of it wasn't implemented correctly so as to work in a Python sub
interpreter. The problems with that would only manifest when using
mod_python or mod_wsgi and application running in sub interpreter and
not main interpreter.

So, to start with, how are you hosting this application?

Graham

Newt

unread,
Apr 25, 2009, 7:13:42 AM4/25/09
to Django users
I'm running it as mod_python on Ubuntu 8.04 server. I connect to
external MSSQL server thru django-pyodbc, the only thing I had to
install manually was the pyodbc, the rest was installed using package
manager (unix-odbc, freetds). I'm not much a server admin and it was a
few months ago.

Ales


On Apr 25, 12:40 pm, Graham Dumpleton <Graham.Dumple...@gmail.com>
wrote:

Graham Dumpleton

unread,
Apr 25, 2009, 7:17:17 AM4/25/09
to Django users
Presuming you are only running the one web application on the server,
do things improve it you add directive:

PythonInterpreter main_interpreter

to Apache configuration.

If it doesn't, not the same issue as pyscopg2.

Graham

Newt

unread,
Apr 25, 2009, 8:06:45 AM4/25/09
to Django users
Thank you very much,
yes, it's the only app (well I have a production and development
version of the same application running there).
The production version is dj 1.0.2
The development version is dj 1.1b - with the trunk version of django-
pyodbc - but I switched to this version in desperate response to this
problem. I hope it's no problem.

Also i found this stuff in vhost configs:
PythonDebug On
PythonAutoReload On

So I commented these and added the interpreter directive. And now I'll
try it...

Ales

On Apr 25, 1:17 pm, Graham Dumpleton <Graham.Dumple...@gmail.com>

Graham Dumpleton

unread,
Apr 25, 2009, 8:37:57 AM4/25/09
to Django users


On Apr 25, 10:06 pm, Newt <strejda.n...@gmail.com> wrote:
> Thank you very much,
> yes, it's the only app (well I have a production and development
> version of the same application running there).
> The production version is dj 1.0.2
> The development version is dj 1.1b - with the trunk version of django-
> pyodbc - but I switched to this version in desperate response to this
> problem. I hope it's no problem.
>
> Also i found this stuff in vhost configs:
>     PythonDebug On
>     PythonAutoReload On

Commenting out PythonAutoReload will not make any difference as On is
the default for mod_python anyway. Also, that option has no bearing on
Django use anyway, and only applies to mod_python's own internal
module importer, which doesn't really get used in any way, at least to
load modules, when Django is being run.

Graham
Reply all
Reply to author
Forward
0 new messages