summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKaren Tracey <kmtracey@gmail.com>2008-11-12 00:38:48 +0000
committerKaren Tracey <kmtracey@gmail.com>2008-11-12 00:38:48 +0000
commit2a1c9072fb7db5085c817912ed8df6c48ed0bb7e (patch)
tree2077fd28acba159d5ea2f2f5e2628a6af81d0a35
parent11736980a4c3ee023ceec55cf47ef3e19818dcac (diff)
[1.0.X] Fixed #5079 -- Avoid converting Decimals to floats during save to the database.
[9394] from trunk. git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.0.X@9395 bcc190cf-cafb-0310-a4f2-bffc1f526a37
-rw-r--r--django/db/backends/util.py7
-rw-r--r--tests/regressiontests/model_fields/models.py11
2 files changed, 16 insertions, 2 deletions
diff --git a/django/db/backends/util.py b/django/db/backends/util.py
index 7228b4046b..f9c07a9741 100644
--- a/django/db/backends/util.py
+++ b/django/db/backends/util.py
@@ -124,4 +124,9 @@ def format_number(value, max_digits, decimal_places):
Formats a number into a string with the requisite number of digits and
decimal places.
"""
- return u"%.*f" % (decimal_places, value)
+ if isinstance(value, decimal.Decimal):
+ context = decimal.getcontext().copy()
+ context.prec = max_digits
+ return u'%s' % str(value.quantize(decimal.Decimal(".1") ** decimal_places, context=context))
+ else:
+ return u"%.*f" % (decimal_places, value)
diff --git a/tests/regressiontests/model_fields/models.py b/tests/regressiontests/model_fields/models.py
index 455e2b3ded..a1c2bb0bc8 100644
--- a/tests/regressiontests/model_fields/models.py
+++ b/tests/regressiontests/model_fields/models.py
@@ -32,6 +32,9 @@ class Whiz(models.Model):
(0,'Other'),
)
c = models.IntegerField(choices=CHOICES, null=True)
+
+class BigD(models.Model):
+ d = models.DecimalField(max_digits=38, decimal_places=30)
__test__ = {'API_TESTS':"""
# Create a couple of Places.
@@ -78,5 +81,11 @@ u''
>>> Foo.objects.filter(d=u'1.23')
[]
-
+# Regression test for #5079 -- ensure decimals don't go through a corrupting
+# float conversion during save.
+>>> bd = BigD(d="12.9")
+>>> bd.save()
+>>> bd = BigD.objects.get(pk=bd.pk)
+>>> bd.d == decimal.Decimal("12.9")
+True
"""}