Home > Programming & scripting, Python > Quick and ugly fix for Data truncated error using FloatField in django 0.96

Quick and ugly fix for Data truncated error using FloatField in django 0.96

August 5th, 2007

I have a FloatFIeld in one of my models that looks lite this

score = models.FloatField(default=0.0, max_digits=2, decimal_places=1)

This would raise this error for me when inserting certain floats (since there are too many decimal places I guess…)

Data truncated for column 'score' at row 1

A very easy fix for this is to make a string representation of the float and then insert that into the FloatField. This works just fine due to “duck typing”. The string quacks like a float ;)

fixed_float = str(my_float)[:3] # only works for floats < 10 obviously

buffi Programming & scripting, Python

  1. August 19th, 2007 at 05:30 | #1

    hi nice post, i enjoyed it

  2. Joakim
    December 23rd, 2007 at 03:45 | #2

    lols! you’re so dirty :/

  1. No trackbacks yet.