Override prepare_value method of RoundingDecimalFormField

- Remove trailing zeros in form field display
This commit is contained in:
Oliver Walters 2020-03-31 21:21:39 +11:00
parent f07f3b99cf
commit 4c96b34c7c

View File

@ -43,7 +43,16 @@ def round_decimal(value, places):
class RoundingDecimalFormField(forms.DecimalField): class RoundingDecimalFormField(forms.DecimalField):
def to_python(self, value): def to_python(self, value):
value = super(RoundingDecimalFormField, self).to_python(value) value = super(RoundingDecimalFormField, self).to_python(value)
return round_decimal(value, self.decimal_places) value = round_decimal(value, self.decimal_places)
return value
def prepare_value(self, value):
"""
Override the 'prepare_value' method, to remove trailing zeros when displaying.
Why? It looks nice!
"""
return value.normalize()
class RoundingDecimalField(models.DecimalField): class RoundingDecimalField(models.DecimalField):