Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #9942 -- Added a to_python handler for FloatField to ensure cor…

…rect typing of deserialized data before saving. Underlying problem is analogous to #8298, fixed in [8515]. Thanks to David Larlet <larlet@gmail.com> for the report and fix.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@9695 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 096a9ecc5cc1e94f3a36c6f3fae967d6d5f1c749 1 parent 3cd1e80
Russell Keith-Magee authored January 03, 2009
1  AUTHORS
@@ -239,6 +239,7 @@ answer newbie questions, and generally made Django that much better:
239 239
     Nick Lane <nick.lane.au@gmail.com>
240 240
     Stuart Langridge <http://www.kryogenix.org/>
241 241
     Paul Lanier <planier@google.com>
  242
+    David Larlet <larlet@gmail.com>
242 243
     Nicola Larosa <nico@teknico.net>
243 244
     Finn Gruwier Larsen <finn@gruwier.dk>
244 245
     Lau Bech Lauritzen
9  django/db/models/fields/__init__.py
@@ -658,6 +658,15 @@ def get_db_prep_value(self, value):
658 658
     def get_internal_type(self):
659 659
         return "FloatField"
660 660
 
  661
+    def to_python(self, value):
  662
+        if value is None:
  663
+            return value
  664
+        try:
  665
+            return float(value)
  666
+        except (TypeError, ValueError):
  667
+            raise exceptions.ValidationError(
  668
+                _("This value must be a float."))
  669
+
661 670
     def formfield(self, **kwargs):
662 671
         defaults = {'form_class': forms.FloatField}
663 672
         defaults.update(kwargs)
1  tests/regressiontests/fixtures_regress/fixtures/animal.xml
@@ -4,5 +4,6 @@
4 4
       <field type="CharField" name="name">Emu</field>
5 5
       <field type="CharField" name="latin_name">Dromaius novaehollandiae</field>
6 6
       <field type="IntegerField" name="count">42</field>
  7
+      <field type="FloatField" name="weight">1.2</field>
7 8
   </object>
8 9
 </django-objects>
3  tests/regressiontests/fixtures_regress/fixtures/sequence.json
@@ -5,7 +5,8 @@
5 5
         "fields": {
6 6
             "name": "Lion", 
7 7
             "latin_name": "Panthera leo",
8  
-            "count": 3
  8
+            "count": 3,
  9
+            "weight": 1.2
9 10
         }
10 11
     }
11 12
 ]
9  tests/regressiontests/fixtures_regress/models.py
@@ -7,6 +7,7 @@ class Animal(models.Model):
7 7
     name = models.CharField(max_length=150)
8 8
     latin_name = models.CharField(max_length=150)
9 9
     count = models.IntegerField()
  10
+    weight = models.FloatField()
10 11
 
11 12
     def __unicode__(self):
12 13
         return self.common_name
@@ -14,6 +15,7 @@ def __unicode__(self):
14 15
 def animal_pre_save_check(signal, sender, instance, **kwargs):
15 16
     "A signal that is used to check the type of data loaded from fixtures"
16 17
     print 'Count = %s (%s)' % (instance.count, type(instance.count))
  18
+    print 'Weight = %s (%s)' % (instance.weight, type(instance.weight))
17 19
 
18 20
 class Plant(models.Model):
19 21
     name = models.CharField(max_length=150)
@@ -69,7 +71,7 @@ class Meta:
69 71
 # Create a new animal. Without a sequence reset, this new object
70 72
 # will take a PK of 1 (on Postgres), and the save will fail.
71 73
 # This is a regression test for ticket #3790.
72  
->>> animal = Animal(name='Platypus', latin_name='Ornithorhynchus anatinus', count=2)
  74
+>>> animal = Animal(name='Platypus', latin_name='Ornithorhynchus anatinus', count=2, weight=2.3)
73 75
 >>> animal.save()
74 76
 
75 77
 ###############################################
@@ -149,12 +151,13 @@ class Meta:
149 151
 [1, 2, 3, 4, 5, 6, 7, 8]
150 152
 
151 153
 ###############################################
152  
-# Test for ticket #8298 - Field values should be coerced into the correct type
153  
-# by the deserializer, not as part of the database write.
  154
+# Test for tickets #8298, #9942 - Field values should be coerced into the
  155
+# correct type by the deserializer, not as part of the database write.
154 156
 
155 157
 >>> models.signals.pre_save.connect(animal_pre_save_check)
156 158
 >>> management.call_command('loaddata', 'animal.xml', verbosity=0)
157 159
 Count = 42 (<type 'int'>)
  160
+Weight = 1.2 (<type 'float'>)
158 161
 
159 162
 >>> models.signals.pre_save.disconnect(animal_pre_save_check)
160 163
 

0 notes on commit 096a9ec

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