Ticket #10933: ticket10933.patch

File ticket10933.patch, 1.8 KB (added by Will Hardy, 13 years ago)

Tests and cleaner fix

  • django/db/models/fields/__init__.py

    diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py
    index 9037265..e6b5400 100644
    a b class DecimalField(Field):  
    768768            return decimal.Decimal(value)
    769769        except decimal.InvalidOperation:
    770770            raise exceptions.ValidationError(self.error_messages['invalid'])
     771        except TypeError:
     772            # Decimal(value) doesn't work when value is a Decimal object after
     773            # the decimal module has been reloaded.
     774            if hasattr(value, 'as_tuple'):
     775                return decimal.Decimal(value.as_tuple())
     776            raise
    771777
    772778    def _format(self, value):
    773779        if isinstance(value, basestring) or value is None:
  • tests/regressiontests/model_fields/tests.py

    diff --git a/tests/regressiontests/model_fields/tests.py b/tests/regressiontests/model_fields/tests.py
    index b42c0af..8bc51dd 100644
    a b class DecimalFieldTests(test.TestCase):  
    6464        self.assertEqual(f.to_python("3.14"), Decimal("3.14"))
    6565        self.assertRaises(ValidationError, f.to_python, "abc")
    6666
     67    def test_to_python_reload(self):
     68        """ Cehcks that a decimal value can still be used after the
     69            module has been reloaded. See ticket #10933.
     70        """
     71        import decimal
     72        f = models.DecimalField(max_digits=4, decimal_places=2)
     73        val = decimal.Decimal(0)
     74        reload(decimal)
     75        try:
     76            self.assertEqual(f.to_python(val), Decimal("0"))
     77        except TypeError:
     78            self.fail("TypeError raised after reloading decimal module.")
     79
    6780    def test_default(self):
    6881        f = models.DecimalField(default=Decimal("0.00"))
    6982        self.assertEqual(f.get_default(), Decimal("0.00"))
Back to Top