#10933: Avoid " TypeError: Cannot convert Decimal("0.0000") to Decimal " when the decimal module has been reloaded ------------------------------------------+--------------------------------- Reporter: gagravarr | Owner: nobody Status: new | Milestone: Component: Database layer (models, ORM) | Version: 1.0 Keywords: | Stage: Unreviewed Has_patch: 1 | ------------------------------------------+--------------------------------- If for some reason the decimal module gets reloaded (seems fairly easy to trigger when using runserver, but we've seen it once or twice wiht apache + mod_python too), then calling db_obj.save() on a model with a decimal field will blow up (see http://groups.google.com/group/django- users/browse_thread/thread/7da92d7f5d6e2a53 for example)
One workaround is to have extra code in the decimal field logic in django, to detect when the value is no longer being recognised as a Decimal but is one, and port it over to the new decimal object. {{{ --- django/db/models/fields/__init__.py (revision 9643) +++ django/db/models/fields/__init__.py (working copy) @@ -579,6 +579,11 @@ def to_python(self, value): if value is None: return value + # Work around reload(decimal) problems + if not isinstance(value, decimal.Decimal) and \ + len(str(value.__class__).split("'")) == 3 and \ + str(value.__class__).split("'")[1] == 'decimal.Decimal': + return decimal.Decimal( value.to_eng_string() ) try: return decimal.Decimal(value) except decimal.InvalidOperation: }}} I'm not sure if this is an ideal fix or not, but it'd be great to have this (or something like it) in django to help avoid the issue -- Ticket URL: <http://code.djangoproject.com/ticket/10933> Django <http://code.djangoproject.com/> The Web framework for perfectionists with deadlines. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django updates" group. To post to this group, send email to django-updates@googlegroups.com To unsubscribe from this group, send email to django-updates+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-updates?hl=en -~----------~----~----~----~------~----~------~--~---