[1.0.X] Fixed #10692 -- Fixed DecimalField lookups for extreme values.

Backport of r10545 from trunk.

git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.0.X@10546 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Malcolm Tredinnick 2009-04-12 05:34:07 +00:00
parent f0c9bc5563
commit 1e0c34dd7f
2 changed files with 25 additions and 10 deletions

View File

@ -611,10 +611,13 @@ class DecimalField(Field):
from django.db.backends import util
return util.format_number(value, self.max_digits, self.decimal_places)
def get_db_prep_value(self, value):
def get_db_prep_save(self, value):
return connection.ops.value_to_db_decimal(self.to_python(value),
self.max_digits, self.decimal_places)
def get_db_prep_value(self, value):
return self.to_python(value)
def formfield(self, **kwargs):
defaults = {
'max_digits': self.max_digits,

View File

@ -1,10 +1,13 @@
import datetime
import unittest
import django.test
from django import forms
from django.db import models
from django.core.exceptions import ValidationError
from models import Foo, Bar, Whiz, BigD, BigS
try:
from decimal import Decimal
except ImportError:
@ -38,7 +41,7 @@ class DecimalFieldTests(django.test.TestCase):
Foo.objects.create(id=1, a='abc', d=Decimal("12.34"))
self.assertEqual(list(Foo.objects.filter(d=u'1.23')), [])
def test_save_wihout_float_conversion(self):
def test_save_without_float_conversion(self):
"""
Ensure decimals don't go through a corrupting float conversion during
save (#5079).
@ -48,6 +51,14 @@ class DecimalFieldTests(django.test.TestCase):
bd = BigD.objects.get(pk=bd.pk)
self.assertEqual(bd.d, Decimal("12.9"))
def test_lookup_really_big_value(self):
"""
Ensure that really big values can be used in a filter statement, even
with older Python versions.
"""
# This should not crash. That counts as a win for our purposes.
Foo.objects.filter(d__gte=100000000000)
class ForeignKeyTests(django.test.TestCase):
def test_callable_default(self):
"""Test the use of a lazy callable for ForeignKey.default"""
@ -120,3 +131,4 @@ class SlugFieldTests(django.test.TestCase):
bs = BigS.objects.create(s = 'slug'*50)
bs = BigS.objects.get(pk=bs.pk)
self.assertEqual(bs.s, 'slug'*50)