Code

Ticket #9942: patch_django_9942.20090102.diff

File patch_django_9942.20090102.diff, 3.7 KB (added by david, 5 years ago)

Patch against r9692

Line 
1Index: django/db/models/fields/__init__.py
2===================================================================
3--- django/db/models/fields/__init__.py (revision 9692)
4+++ django/db/models/fields/__init__.py (working copy)
5@@ -658,6 +658,15 @@
6     def get_internal_type(self):
7         return "FloatField"
8 
9+    def to_python(self, value):
10+        if value is None:
11+            return value
12+        try:
13+            return float(value)
14+        except (TypeError, ValueError):
15+            raise exceptions.ValidationError(
16+                _("This value must be a float."))
17+
18     def formfield(self, **kwargs):
19         defaults = {'form_class': forms.FloatField}
20         defaults.update(kwargs)
21
22Index: tests/regressiontests/fixtures_regress/fixtures/sequence.json
23===================================================================
24--- tests/regressiontests/fixtures_regress/fixtures/sequence.json       (revision 9692)
25+++ tests/regressiontests/fixtures_regress/fixtures/sequence.json       (working copy)
26@@ -5,7 +5,8 @@
27         "fields": {
28             "name": "Lion",
29             "latin_name": "Panthera leo",
30-            "count": 3
31+            "count": 3,
32+            "weight": 1.2
33         }
34     }
35 ]
36\ No newline at end of file
37Index: tests/regressiontests/fixtures_regress/fixtures/animal.xml
38===================================================================
39--- tests/regressiontests/fixtures_regress/fixtures/animal.xml  (revision 9692)
40+++ tests/regressiontests/fixtures_regress/fixtures/animal.xml  (working copy)
41@@ -4,5 +4,6 @@
42       <field type="CharField" name="name">Emu</field>
43       <field type="CharField" name="latin_name">Dromaius novaehollandiae</field>
44       <field type="IntegerField" name="count">42</field>
45+      <field type="FloatField" name="weight">1.2</field>
46   </object>
47 </django-objects>
48\ No newline at end of file
49Index: tests/regressiontests/fixtures_regress/models.py
50===================================================================
51--- tests/regressiontests/fixtures_regress/models.py    (revision 9692)
52+++ tests/regressiontests/fixtures_regress/models.py    (working copy)
53@@ -7,6 +7,7 @@
54     name = models.CharField(max_length=150)
55     latin_name = models.CharField(max_length=150)
56     count = models.IntegerField()
57+    weight = models.FloatField()
58 
59     def __unicode__(self):
60         return self.common_name
61@@ -14,6 +15,7 @@
62 def animal_pre_save_check(signal, sender, instance, **kwargs):
63     "A signal that is used to check the type of data loaded from fixtures"
64     print 'Count = %s (%s)' % (instance.count, type(instance.count))
65+    print 'Weight = %s (%s)' % (instance.weight, type(instance.weight))
66 
67 class Plant(models.Model):
68     name = models.CharField(max_length=150)
69@@ -69,7 +71,7 @@
70 # Create a new animal. Without a sequence reset, this new object
71 # will take a PK of 1 (on Postgres), and the save will fail.
72 # This is a regression test for ticket #3790.
73->>> animal = Animal(name='Platypus', latin_name='Ornithorhynchus anatinus', count=2)
74+>>> animal = Animal(name='Platypus', latin_name='Ornithorhynchus anatinus', count=2, weight=2.3)
75 >>> animal.save()
76 
77 ###############################################
78@@ -149,12 +151,13 @@
79 [1, 2, 3, 4, 5, 6, 7, 8]
80 
81 ###############################################
82-# Test for ticket #8298 - Field values should be coerced into the correct type
83-# by the deserializer, not as part of the database write.
84+# Test for tickets #8298, #9942 - Field values should be coerced into the
85+# correct type by the deserializer, not as part of the database write.
86 
87 >>> models.signals.pre_save.connect(animal_pre_save_check)
88 >>> management.call_command('loaddata', 'animal.xml', verbosity=0)
89 Count = 42 (<type 'int'>)
90+Weight = 1.2 (<type 'float'>)
91 
92 >>> models.signals.pre_save.disconnect(animal_pre_save_check)
93