I'm attempting to create a custom form field that works the same as float field for all intents and purposes, but that (by default) outputs the float value with no trailing zeros e.g. 33 rather than 33.0
I attempted to simply extend django.forms.FloatField like so:
class CustomFloatField(django.forms.FloatField):
def to_python(self, value):
"""
Returns the value without trailing zeros.
"""
value = super(django.forms.FloatField, self).to_python(value)
# code to strip trailing zeros
return stripped_value
But this ended up with me getting validation errors. When I looked closer at the FloatField class I noticed that in its own to_python() method it calls super(IntegerField, self).to_python(value) which checks to ensure the value can be cast to an int, and it was here that my code seemed to trip up. This has left me thoroughly confused. How does FloatField work at all if it has to try and cast it's value to an int? :)
Most likely I'm barking entirely up the wrong tree here but if someone could point me in the right direction I'd be grateful.