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):
|
768 | 768 | return decimal.Decimal(value) |
769 | 769 | except decimal.InvalidOperation: |
770 | 770 | 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 |
771 | 777 | |
772 | 778 | def _format(self, value): |
773 | 779 | if isinstance(value, basestring) or value is None: |
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):
|
64 | 64 | self.assertEqual(f.to_python("3.14"), Decimal("3.14")) |
65 | 65 | self.assertRaises(ValidationError, f.to_python, "abc") |
66 | 66 | |
| 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 | |
67 | 80 | def test_default(self): |
68 | 81 | f = models.DecimalField(default=Decimal("0.00")) |
69 | 82 | self.assertEqual(f.get_default(), Decimal("0.00")) |