Automatically round Django's DecimalField according to the max_digits and decimal_places attributes before calling save()

You are mainly getting the error because forms.DecimalField has separate validators from models.DecimalField:

data = {'amount': 1.12345 }

class NormalForm(forms.Form):
    amount = forms.DecimalField(max_digits = 19, decimal_places = 2)

normal_form = NormalForm(data)
normal_form.is_valid()  # returns False
normal_form.cleaned_data  # returns {}

and forms.DecimalField is used by default for forms for models with fields of class models.DecimalField. You could do something like this:

from django import forms
from django.db import models
from decimal import Decimal

def round_decimal(value, places):
    if value is not None:
        # see https://docs.python.org/2/library/decimal.html#decimal.Decimal.quantize for options
        return value.quantize(Decimal(10) ** -places)
    return value

class RoundingDecimalFormField(forms.DecimalField):
    def to_python(self, value):
        value = super(RoundingDecimalFormField, self).to_python(value)
        return round_decimal(value, self.decimal_places)

class RoundingDecimalModelField(models.DecimalField):
    def to_python(self, value):
        # you could actually skip implementing this
        value = super(RoundingDecimalModelField, self).to_python(value)
        return round_decimal(value, self.decimal_places)

    def formfield(self, **kwargs):
        defaults = { 'form_class': RoundingDecimalFormField }
        defaults.update(kwargs)
        return super(RoundingDecimalModelField, self).formfield(**kwargs)

Now anywhere you are using models.DecimalField, use RoundingDecimalModelField instead. Any form you use with those models will now also use the custom form field.

class RoundingForm(forms.Form):
    amount = RoundingDecimalFormField(max_digits = 19, decimal_places = 2)

data = {'amount': 1.12345 }

rounding_form = RoundingForm(data)
rounding_form.is_valid()  # returns True
rounding_form.cleaned_data  # returns {'amount': Decimal('1.12')}

If you are assigning directly to a model instance, you don't need to worry about it. The field object will quantize the value (rounding it) to the decimal point level you set in your model definition.

If you are dealing with a ModelForm, the default DecimalField will require that any input match the model field's decimal points. The easiest way to handle this in general is probably to subclass the model DecimalField, removing the decimal-specific validator and relying on the underlying conversion to quantize the data, with something like this:

from django.db.models.fields import DecimalField

class RoundingDecimalField(DecimalField):

    @cached_property
    def validators(self):
        return super(DecimalField, self).validators

    def formfield(self, **kwargs):
        defaults = {
            'max_digits': self.max_digits,
            'decimal_places': 4, # or whatever number of decimal places you want your form to accept, make it a param if you like
            'form_class': forms.DecimalField,
        }
        defaults.update(kwargs)
        return super(RoundingDecimalField, self).formfield(**defaults)

Then in your models:

amount = RoundingDecimalField(max_digits = 19, decimal_places = 2)

(Don't actually put the field class in the same field as the model, that's just for example.)

This is probably less correct in absolute terms than defining a custom field form, which was my first suggestion, but is less work to use.

Tags:

Python

Django