Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #8298: Added a to_python method for integer fields. This ensure…

…s that the data from deserialized instances is of correct type prior to saving. Thanks to Andrew Badr for the report.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@8515 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 11e43883d55a17e39791f961aff952ef9e2c3ec5 1 parent e822fd7
Russell Keith-Magee authored August 24, 2008
8  django/db/models/fields/__init__.py
@@ -786,6 +786,14 @@ def get_manipulator_field_objs(self):
786 786
     def get_internal_type(self):
787 787
         return "IntegerField"
788 788
 
  789
+    def to_python(self, value):
  790
+        if value is None:
  791
+            return value
  792
+        try:
  793
+            return int(value)
  794
+        except (TypeError, ValueError):
  795
+            raise validators.ValidationError, _("This value must be an integer.")
  796
+                
789 797
     def formfield(self, **kwargs):
790 798
         defaults = {'form_class': forms.IntegerField}
791 799
         defaults.update(kwargs)
8  tests/regressiontests/fixtures_regress/fixtures/animal.xml
... ...
@@ -0,0 +1,8 @@
  1
+<?xml version="1.0" encoding="utf-8"?>
  2
+<django-objects version="1.0">
  3
+  <object pk="10" model="fixtures_regress.animal">
  4
+      <field type="CharField" name="name">Emu</field>
  5
+      <field type="CharField" name="latin_name">Dromaius novaehollandiae</field>
  6
+      <field type="IntegerField" name="count">42</field>
  7
+  </object>
  8
+</django-objects>
3  tests/regressiontests/fixtures_regress/fixtures/sequence.json
@@ -4,7 +4,8 @@
4 4
         "model": "fixtures_regress.animal", 
5 5
         "fields": {
6 6
             "name": "Lion", 
7  
-            "latin_name": "Panthera leo"
  7
+            "latin_name": "Panthera leo",
  8
+            "count": 3
8 9
         }
9 10
     }
10 11
 ]
19  tests/regressiontests/fixtures_regress/models.py
@@ -6,10 +6,15 @@
6 6
 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
+    
10 11
     def __unicode__(self):
11 12
         return self.common_name
12 13
 
  14
+def animal_pre_save_check(signal, sender, instance, **kwargs):
  15
+    "A signal that is used to check the type of data loaded from fixtures"
  16
+    print 'Count = %s (%s)' % (instance.count, type(instance.count))
  17
+
13 18
 class Plant(models.Model):
14 19
     name = models.CharField(max_length=150)
15 20
 
@@ -64,7 +69,7 @@ class Meta:
64 69
 # Create a new animal. Without a sequence reset, this new object
65 70
 # will take a PK of 1 (on Postgres), and the save will fail.
66 71
 # This is a regression test for ticket #3790.
67  
->>> animal = Animal(name='Platypus', latin_name='Ornithorhynchus anatinus')
  72
+>>> animal = Animal(name='Platypus', latin_name='Ornithorhynchus anatinus', count=2)
68 73
 >>> animal.save()
69 74
 
70 75
 ###############################################
@@ -134,4 +139,14 @@ class Meta:
134 139
 >>> articles.values_list('id', flat=True)
135 140
 [1, 2, 3, 4, 5, 6, 7, 8]
136 141
 
  142
+###############################################
  143
+# Test for ticket #8298 - Field values should be coerced into the correct type
  144
+# by the deserializer, not as part of the database write.
  145
+
  146
+>>> models.signals.pre_save.connect(animal_pre_save_check)
  147
+>>> management.call_command('loaddata', 'animal.xml', verbosity=0)
  148
+Count = 42 (<type 'int'>)
  149
+
  150
+>>> models.signals.pre_save.disconnect(animal_pre_save_check)
  151
+
137 152
 """}

0 notes on commit 11e4388

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