Skip to content

Commit

Permalink
Fixed #8298: Added a to_python method for integer fields. This ensure…
Browse files Browse the repository at this point in the history
…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
freakboy3742 committed Aug 24, 2008
1 parent e822fd7 commit 11e4388
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 3 deletions.
8 changes: 8 additions & 0 deletions django/db/models/fields/__init__.py
Expand Up @@ -786,6 +786,14 @@ def get_manipulator_field_objs(self):
def get_internal_type(self): def get_internal_type(self):
return "IntegerField" return "IntegerField"


def to_python(self, value):
if value is None:
return value
try:
return int(value)
except (TypeError, ValueError):
raise validators.ValidationError, _("This value must be an integer.")

def formfield(self, **kwargs): def formfield(self, **kwargs):
defaults = {'form_class': forms.IntegerField} defaults = {'form_class': forms.IntegerField}
defaults.update(kwargs) defaults.update(kwargs)
Expand Down
8 changes: 8 additions & 0 deletions tests/regressiontests/fixtures_regress/fixtures/animal.xml
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<django-objects version="1.0">
<object pk="10" model="fixtures_regress.animal">
<field type="CharField" name="name">Emu</field>
<field type="CharField" name="latin_name">Dromaius novaehollandiae</field>
<field type="IntegerField" name="count">42</field>
</object>
</django-objects>
Expand Up @@ -4,7 +4,8 @@
"model": "fixtures_regress.animal", "model": "fixtures_regress.animal",
"fields": { "fields": {
"name": "Lion", "name": "Lion",
"latin_name": "Panthera leo" "latin_name": "Panthera leo",
"count": 3
} }
} }
] ]
19 changes: 17 additions & 2 deletions tests/regressiontests/fixtures_regress/models.py
Expand Up @@ -6,10 +6,15 @@
class Animal(models.Model): class Animal(models.Model):
name = models.CharField(max_length=150) name = models.CharField(max_length=150)
latin_name = models.CharField(max_length=150) latin_name = models.CharField(max_length=150)

count = models.IntegerField()

def __unicode__(self): def __unicode__(self):
return self.common_name return self.common_name


def animal_pre_save_check(signal, sender, instance, **kwargs):
"A signal that is used to check the type of data loaded from fixtures"
print 'Count = %s (%s)' % (instance.count, type(instance.count))

class Plant(models.Model): class Plant(models.Model):
name = models.CharField(max_length=150) name = models.CharField(max_length=150)


Expand Down Expand Up @@ -64,7 +69,7 @@ class Meta:
# Create a new animal. Without a sequence reset, this new object # Create a new animal. Without a sequence reset, this new object
# will take a PK of 1 (on Postgres), and the save will fail. # will take a PK of 1 (on Postgres), and the save will fail.
# This is a regression test for ticket #3790. # This is a regression test for ticket #3790.
>>> animal = Animal(name='Platypus', latin_name='Ornithorhynchus anatinus') >>> animal = Animal(name='Platypus', latin_name='Ornithorhynchus anatinus', count=2)
>>> animal.save() >>> animal.save()
############################################### ###############################################
Expand Down Expand Up @@ -134,4 +139,14 @@ class Meta:
>>> articles.values_list('id', flat=True) >>> articles.values_list('id', flat=True)
[1, 2, 3, 4, 5, 6, 7, 8] [1, 2, 3, 4, 5, 6, 7, 8]
###############################################
# Test for ticket #8298 - Field values should be coerced into the correct type
# by the deserializer, not as part of the database write.
>>> models.signals.pre_save.connect(animal_pre_save_check)
>>> management.call_command('loaddata', 'animal.xml', verbosity=0)
Count = 42 (<type 'int'>)
>>> models.signals.pre_save.disconnect(animal_pre_save_check)
"""} """}

0 comments on commit 11e4388

Please sign in to comment.