Ticket #9942: patch_django_9942.20090102.diff

File patch_django_9942.20090102.diff, 3.7 KB (added by David Larlet, 15 years ago)

Patch against r9692

  • django/db/models/fields/__init__.py

     
    658658    def get_internal_type(self):
    659659        return "FloatField"
    660660
     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
    661670    def formfield(self, **kwargs):
    662671        defaults = {'form_class': forms.FloatField}
    663672        defaults.update(kwargs)
  • tests/regressiontests/fixtures_regress/fixtures/sequence.json

     
    55        "fields": {
    66            "name": "Lion",
    77            "latin_name": "Panthera leo",
    8             "count": 3
     8            "count": 3,
     9            "weight": 1.2
    910        }
    1011    }
    1112]
     13 No newline at end of file
  • tests/regressiontests/fixtures_regress/fixtures/animal.xml

     
    44      <field type="CharField" name="name">Emu</field>
    55      <field type="CharField" name="latin_name">Dromaius novaehollandiae</field>
    66      <field type="IntegerField" name="count">42</field>
     7      <field type="FloatField" name="weight">1.2</field>
    78  </object>
    89</django-objects>
     10 No newline at end of file
  • tests/regressiontests/fixtures_regress/models.py

     
    77    name = models.CharField(max_length=150)
    88    latin_name = models.CharField(max_length=150)
    99    count = models.IntegerField()
     10    weight = models.FloatField()
    1011
    1112    def __unicode__(self):
    1213        return self.common_name
     
    1415def animal_pre_save_check(signal, sender, instance, **kwargs):
    1516    "A signal that is used to check the type of data loaded from fixtures"
    1617    print 'Count = %s (%s)' % (instance.count, type(instance.count))
     18    print 'Weight = %s (%s)' % (instance.weight, type(instance.weight))
    1719
    1820class Plant(models.Model):
    1921    name = models.CharField(max_length=150)
     
    6971# Create a new animal. Without a sequence reset, this new object
    7072# will take a PK of 1 (on Postgres), and the save will fail.
    7173# 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)
    7375>>> animal.save()
    7476
    7577###############################################
     
    149151[1, 2, 3, 4, 5, 6, 7, 8]
    150152
    151153###############################################
    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.
    154156
    155157>>> models.signals.pre_save.connect(animal_pre_save_check)
    156158>>> management.call_command('loaddata', 'animal.xml', verbosity=0)
    157159Count = 42 (<type 'int'>)
     160Weight = 1.2 (<type 'float'>)
    158161
    159162>>> models.signals.pre_save.disconnect(animal_pre_save_check)
    160163
Back to Top