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
hi nice post, i enjoyed it
lols! you’re so dirty :/