"Cannot convert Decimal to Decimal" Probably not a Django issue but hoped someone knows

328 views
Skip to first unread message

Norman Harman

unread,
Aug 5, 2008, 3:18:57 PM8/5/08
to django...@googlegroups.com
Good day,

Weirdo error starting happening after upping to -r8161 of trunk. There
were also upgrades to my djanogo code and library(yum update).

I get TypeError: Cannot convert Decimal("0.00") <class
'decimal.Decimal'> to Decimal error only sometimes when trying to save
model that includes this field.

cost = models.DecimalField("Cost", max_digits=10, decimal_places=2,
default=Decimal("0.00"))

Doing this in Python shell on same machine has no problems.

>>> from decimal import Decimal
>>> d = Decimal("1.3423")
>>> a = Decimal(d)
>>> a
Decimal("1.3423")


So, something bizzaro is going on in django/mod_python/psycopg2/???.
Googling around hasn't turned up any answers.

Here's the full traceback: [I added the type to error output since I
thought perhaps it was the literal string 'Decimal("0.00")' and not a
Decimal instance.]

File
"/usr/local/siteuser/vhosts/webapps.statesman.com/python/django/core/handlers/base.py",
line 87, in get_response
response = callback(request, *callback_args, **callback_kwargs)

File
"/usr/local/siteuser/vhosts/webapps.statesman.com/python/aas/urlconf.py",
line 29, in func
return site.root(request, "%s%s" % (app_model_url, rest_of_url))

File
"/usr/local/siteuser/vhosts/webapps.statesman.com/python/django/contrib/admin/sites.py",
line 159, in root
return self.model_page(request, *url.split('/', 2))

File
"/usr/local/siteuser/vhosts/webapps.statesman.com/python/django/views/decorators/cache.py",
line 44, in _wrapped_view_func
response = view_func(request, *args, **kwargs)

File
"/usr/local/siteuser/vhosts/webapps.statesman.com/python/django/contrib/admin/sites.py",
line 176, in model_page
return admin_obj(request, rest_of_url)

File
"/usr/local/siteuser/vhosts/webapps.statesman.com/python/django/contrib/admin/options.py",
line 256, in __call__
return self.add_view(request)

File
"/usr/local/siteuser/vhosts/webapps.statesman.com/python/django/contrib/admin/options.py",
line 498, in add_view
return self.save_add(request, form, inline_formsets, '../%s/')

File
"/usr/local/siteuser/vhosts/webapps.statesman.com/python/django/db/transaction.py",
line 198, in _commit_on_success
res = func(*args, **kw)

File
"/usr/local/siteuser/vhosts/webapps.statesman.com/python/django/contrib/admin/options.py",
line 354, in save_add
formset.save()

File
"/usr/local/siteuser/vhosts/webapps.statesman.com/python/django/forms/models.py",
line 339, in save
return self.save_existing_objects(commit) +
self.save_new_objects(commit)

File
"/usr/local/siteuser/vhosts/webapps.statesman.com/python/django/forms/models.py",
line 374, in save_new_objects
self.new_objects.append(self.save_new(form, commit=commit))

File
"/usr/local/siteuser/vhosts/webapps.statesman.com/python/django/forms/models.py",
line 430, in save_new
return save_instance(form, new_obj, commit=commit)

File
"/usr/local/siteuser/vhosts/webapps.statesman.com/python/django/forms/models.py",
line 57, in save_instance
instance.save()

File
"/usr/local/siteuser/vhosts/webapps.statesman.com/python/specials/models.py",
line 317, in save
self._update_first_rundate()

File
"/usr/local/siteuser/vhosts/webapps.statesman.com/python/specials/models.py",
line 312, in _update_first_rundate
ad.save()

File
"/usr/local/siteuser/vhosts/webapps.statesman.com/python/specials/models.py",
line 212, in save
super(AdSpecial, self).save()

File
"/usr/local/siteuser/vhosts/webapps.statesman.com/python/django/db/models/base.py",
line 274, in save
self.save_base()

File
"/usr/local/siteuser/vhosts/webapps.statesman.com/python/django/db/models/base.py",
line 324, in save_base
values = [(f, None, f.get_db_prep_save(raw and getattr(self,
f.attname) or f.pre_save(self, False))) for f in non_pks]

File
"/usr/local/siteuser/vhosts/webapps.statesman.com/python/django/db/models/fields/__init__.py",
line 232, in get_db_prep_save
return self.get_db_prep_value(value)

File
"/usr/local/siteuser/vhosts/webapps.statesman.com/python/django/db/models/fields/__init__.py",
line 735, in get_db_prep_value
return connection.ops.value_to_db_decimal(self.to_python(value),

File
"/usr/local/siteuser/vhosts/webapps.statesman.com/python/django/db/models/fields/__init__.py",
line 709, in to_python
return decimal.Decimal(value)

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

TypeError: Cannot convert Decimal("0.00") <class 'decimal.Decimal'> to
Decimal


Thanks,
--
Norman J. Harman Jr.
Senior Web Specialist, Austin American-Statesman
___________________________________________________________________________
It's August! Keep your cool with the Statesman. Check out our hot
deals in print and online for back-to-school savings and more!

Whit

unread,
Aug 5, 2008, 3:38:35 PM8/5/08
to Django users
Hi,

It's a psycopg's problem. Solved in psycopg 2.0.7.

Nice day, Vitek

Norman Harman napsal(a):

Norman Harman

unread,
Aug 5, 2008, 4:02:19 PM8/5/08
to django...@googlegroups.com
Whit wrote:
> Hi,
>
> It's a psycopg's problem. Solved in psycopg 2.0.7.

Super awesome thanks!

> Nice day, Vitek

I will, now.

jme

unread,
Aug 16, 2008, 1:37:46 PM8/16/08
to Django users
Hi,

I'm seeing this same problem, but i'm using psycopg1, does anyone know
what the actual source of the problem is? I can't find what it might
be in the psycopg2 changelogs to figure out how it's affecting
psycopg1.

Thanks,

Jamie

Devil Landlord

unread,
Sep 1, 2008, 5:51:32 AM9/1/08
to django...@googlegroups.com
You're using PsycoPG with multiple interpreters.

I had the same problem:
http://groups.google.com/group/django-users/browse_thread/thread/91aa6c088f6c090/929f2fd69307cd22

There is a reference to the psycopg2 ticket with patch attached to it, unfortunately the psycopg site appears to be down due to maintenance... So I am attaching a patch that I am using (not sure that this is the patch included with the official 2.0.7).
decimal_and_decimal.diff
Reply all
Reply to author
Forward
0 new messages