Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #4485 -- Allow nullable DecimalFields to store NULLs.

Based on a patch from tdterry. Thanks.


git-svn-id: http://code.djangoproject.com/svn/django/trunk@7797 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 52cc11c4e24be0a108d3f778ed546985fc608535 1 parent a254f12
Malcolm Tredinnick authored June 30, 2008
5  django/db/models/fields/__init__.py
@@ -695,7 +695,7 @@ def to_python(self, value):
695 695
                 _("This value must be a decimal number."))
696 696
 
697 697
     def _format(self, value):
698  
-        if isinstance(value, basestring):
  698
+        if isinstance(value, basestring) or value is None:
699 699
             return value
700 700
         else:
701 701
             return self.format_number(value)
@@ -716,8 +716,7 @@ def format_number(self, value):
716 716
         return u"%.*f" % (self.decimal_places, value)
717 717
 
718 718
     def get_db_prep_save(self, value):
719  
-        if value is not None:
720  
-            value = self._format(value)
  719
+        value = self._format(value)
721 720
         return super(DecimalField, self).get_db_prep_save(value)
722 721
 
723 722
     def get_db_prep_lookup(self, lookup_type, value):
17  tests/regressiontests/model_fields/tests.py
@@ -15,4 +15,21 @@
15 15
 Traceback (most recent call last):
16 16
 ...
17 17
 ValidationError: [u'This value must be a decimal number.']
  18
+
  19
+>>> f = DecimalField(max_digits=5, decimal_places=1)
  20
+>>> x = f.to_python(2)
  21
+>>> y = f.to_python('2.6')
  22
+
  23
+>>> f.get_db_prep_save(x)
  24
+u'2.0'
  25
+>>> f.get_db_prep_save(y)
  26
+u'2.6'
  27
+>>> f.get_db_prep_save(None)
  28
+>>> f.get_db_prep_lookup('exact', x)
  29
+[u'2.0']
  30
+>>> f.get_db_prep_lookup('exact', y)
  31
+[u'2.6']
  32
+>>> f.get_db_prep_lookup('exact', None)
  33
+[None]
  34
+
18 35
 """

0 notes on commit 52cc11c

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