mirror of https://github.com/django/django.git
Fixed #9942 -- Added a to_python handler for FloatField to ensure correct 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
This commit is contained in:
parent
3cd1e80ae4
commit
096a9ecc5c
1
AUTHORS
1
AUTHORS
|
@ -239,6 +239,7 @@ answer newbie questions, and generally made Django that much better:
|
||||||
Nick Lane <nick.lane.au@gmail.com>
|
Nick Lane <nick.lane.au@gmail.com>
|
||||||
Stuart Langridge <http://www.kryogenix.org/>
|
Stuart Langridge <http://www.kryogenix.org/>
|
||||||
Paul Lanier <planier@google.com>
|
Paul Lanier <planier@google.com>
|
||||||
|
David Larlet <larlet@gmail.com>
|
||||||
Nicola Larosa <nico@teknico.net>
|
Nicola Larosa <nico@teknico.net>
|
||||||
Finn Gruwier Larsen <finn@gruwier.dk>
|
Finn Gruwier Larsen <finn@gruwier.dk>
|
||||||
Lau Bech Lauritzen
|
Lau Bech Lauritzen
|
||||||
|
|
|
@ -658,6 +658,15 @@ class FloatField(Field):
|
||||||
def get_internal_type(self):
|
def get_internal_type(self):
|
||||||
return "FloatField"
|
return "FloatField"
|
||||||
|
|
||||||
|
def to_python(self, value):
|
||||||
|
if value is None:
|
||||||
|
return value
|
||||||
|
try:
|
||||||
|
return float(value)
|
||||||
|
except (TypeError, ValueError):
|
||||||
|
raise exceptions.ValidationError(
|
||||||
|
_("This value must be a float."))
|
||||||
|
|
||||||
def formfield(self, **kwargs):
|
def formfield(self, **kwargs):
|
||||||
defaults = {'form_class': forms.FloatField}
|
defaults = {'form_class': forms.FloatField}
|
||||||
defaults.update(kwargs)
|
defaults.update(kwargs)
|
||||||
|
|
|
@ -4,5 +4,6 @@
|
||||||
<field type="CharField" name="name">Emu</field>
|
<field type="CharField" name="name">Emu</field>
|
||||||
<field type="CharField" name="latin_name">Dromaius novaehollandiae</field>
|
<field type="CharField" name="latin_name">Dromaius novaehollandiae</field>
|
||||||
<field type="IntegerField" name="count">42</field>
|
<field type="IntegerField" name="count">42</field>
|
||||||
|
<field type="FloatField" name="weight">1.2</field>
|
||||||
</object>
|
</object>
|
||||||
</django-objects>
|
</django-objects>
|
|
@ -5,7 +5,8 @@
|
||||||
"fields": {
|
"fields": {
|
||||||
"name": "Lion",
|
"name": "Lion",
|
||||||
"latin_name": "Panthera leo",
|
"latin_name": "Panthera leo",
|
||||||
"count": 3
|
"count": 3,
|
||||||
|
"weight": 1.2
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
]
|
]
|
|
@ -7,6 +7,7 @@ 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()
|
count = models.IntegerField()
|
||||||
|
weight = models.FloatField()
|
||||||
|
|
||||||
def __unicode__(self):
|
def __unicode__(self):
|
||||||
return self.common_name
|
return self.common_name
|
||||||
|
@ -14,6 +15,7 @@ class Animal(models.Model):
|
||||||
def animal_pre_save_check(signal, sender, instance, **kwargs):
|
def animal_pre_save_check(signal, sender, instance, **kwargs):
|
||||||
"A signal that is used to check the type of data loaded from fixtures"
|
"A signal that is used to check the type of data loaded from fixtures"
|
||||||
print 'Count = %s (%s)' % (instance.count, type(instance.count))
|
print 'Count = %s (%s)' % (instance.count, type(instance.count))
|
||||||
|
print 'Weight = %s (%s)' % (instance.weight, type(instance.weight))
|
||||||
|
|
||||||
class Plant(models.Model):
|
class Plant(models.Model):
|
||||||
name = models.CharField(max_length=150)
|
name = models.CharField(max_length=150)
|
||||||
|
@ -69,7 +71,7 @@ __test__ = {'API_TESTS':"""
|
||||||
# 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', count=2)
|
>>> animal = Animal(name='Platypus', latin_name='Ornithorhynchus anatinus', count=2, weight=2.3)
|
||||||
>>> animal.save()
|
>>> animal.save()
|
||||||
|
|
||||||
###############################################
|
###############################################
|
||||||
|
@ -149,12 +151,13 @@ No fixture data found for 'bad_fixture2'. (File format may be invalid.)
|
||||||
[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
|
# Test for tickets #8298, #9942 - Field values should be coerced into the
|
||||||
# by the deserializer, not as part of the database write.
|
# correct type by the deserializer, not as part of the database write.
|
||||||
|
|
||||||
>>> models.signals.pre_save.connect(animal_pre_save_check)
|
>>> models.signals.pre_save.connect(animal_pre_save_check)
|
||||||
>>> management.call_command('loaddata', 'animal.xml', verbosity=0)
|
>>> management.call_command('loaddata', 'animal.xml', verbosity=0)
|
||||||
Count = 42 (<type 'int'>)
|
Count = 42 (<type 'int'>)
|
||||||
|
Weight = 1.2 (<type 'float'>)
|
||||||
|
|
||||||
>>> models.signals.pre_save.disconnect(animal_pre_save_check)
|
>>> models.signals.pre_save.disconnect(animal_pre_save_check)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue