Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #8023 -- Allow filtering of DecimalFields (in models) using str…

…ings.

At the same time, checked all other cases of db_prep_value() to ensure they
aren't making the same mistake.


git-svn-id: http://code.djangoproject.com/svn/django/trunk@8143 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 281f2b74bf19161a0dc5791a07d239ce6a27ed2f 1 parent 1ba2d68
Malcolm Tredinnick authored
4  django/db/models/fields/__init__.py
@@ -732,8 +732,8 @@ def format_number(self, value):
732 732
         return util.format_number(value, self.max_digits, self.decimal_places)
733 733
 
734 734
     def get_db_prep_value(self, value):
735  
-        return connection.ops.value_to_db_decimal(value, self.max_digits,
736  
-                                                  self.decimal_places)
  735
+        return connection.ops.value_to_db_decimal(self.to_python(value),
  736
+                self.max_digits, self.decimal_places)
737 737
 
738 738
     def get_manipulator_field_objs(self):
739 739
         return [curry(oldforms.DecimalField, max_digits=self.max_digits, decimal_places=self.decimal_places)]
17  tests/regressiontests/model_fields/models.py
... ...
@@ -1,8 +1,14 @@
1 1
 
2 2
 from django.db import models
3 3
 
  4
+try:
  5
+    import decimal
  6
+except ImportError:
  7
+    from django.utils import _decimal as decimal    # Python 2.3 fallback
  8
+
4 9
 class Foo(models.Model):
5 10
     a = models.CharField(max_length=10)
  11
+    d = models.DecimalField(max_digits=5, decimal_places=3)
6 12
 
7 13
 def get_foo():
8 14
     return Foo.objects.get(id=1)
@@ -22,14 +28,14 @@ class Whiz(models.Model):
22 28
                 (3,'Third'),
23 29
                 (4,'Fourth'),
24 30
             )
25  
-        ),        
  31
+        ),
26 32
         (0,'Other'),
27 33
     )
28 34
     c = models.IntegerField(choices=CHOICES, null=True)
29  
-    
  35
+
30 36
 __test__ = {'API_TESTS':"""
31 37
 # Create a couple of Places.
32  
->>> f = Foo.objects.create(a='abc')
  38
+>>> f = Foo.objects.create(a='abc', d=decimal.Decimal("12.34"))
33 39
 >>> f.id
34 40
 1
35 41
 >>> b = Bar(b = "bcd")
@@ -67,5 +73,10 @@ class Whiz(models.Model):
67 73
 >>> w.get_c_display()
68 74
 u''
69 75
 
  76
+# Regression test for #8023: should be able to filter decimal fields using
  77
+# strings (which is what gets passed through from, e.g., the admin interface).
  78
+>>> Foo.objects.filter(d=u'1.23')
  79
+[]
  80
+
70 81
 
71 82
 """}

0 notes on commit 281f2b7

Please sign in to comment.
Something went wrong with that request. Please try again.