0

I'm trying to force Django to always store numbers with 2 decimal places but if the number is a whole one then it isn't store the .00

def clean_myfield(self):
    data = self.cleaned_data['myfield']
    self.cleandecimal(data)
    return data

def cleandecimal(self, data):
    data = Decimal(data).quantize(Decimal('.01'), rounding=ROUND_DOWN)
    print 'data ', data
    return data

My prints always display as intended e.g. 4.00 but that gets stored as 4

How can I override the save method to store the value without rounding it?

3
  • What type is the field in the model? Oct 3, 2012 at 10:51
  • sorry, they're all standard models.DecimalField or a custom type derived from models.DecimalField Oct 3, 2012 at 10:55
  • Why is it a problem that its storing it as 4? As long as it doesn't store 4.10 as 4, I don't see the issue here. Oct 3, 2012 at 11:12

2 Answers 2

2

Its a representation issue. Decimal number stores numbers, not representations of them. Mathematically 1.00 is equal to 1. So its possible that your database backend stores smallest truncated decimal without all zeros - so it's probably not Django, but db. If you want to force Django to always return decimal with two decimal places, you can overwrite the DecimalField:

class MyDecimalField(models.DecimalField):
    __metaclass__ = models.SubfieldBase

    def __init__(self, *args, **kwargs):
        super(self.__class__, self).__init__(*args, **kwargs)
        places = kwargs.get('decimal_places', 2)
        self.q = Decimal(10) ** -places

    def to_python(self, value):
        value = super(self.__class__, self).to_python(value)

        if isinstance(value, Decimal):
            return value.quantize(self.q)

        else:
            return value

This will always return numbers with decimal_places as specified. You can use it the same way as you would normal DecimalField, like:

class FooModle(models.Model)
    number = MyDecimalField(decimal_places=2, max_digits=10)
1
  • Yeah after trying to enter a value in the db itself I thought it was db related. Thanks, I'll give that that a try now. Oct 3, 2012 at 12:05
0

Use models.DecimalField(max_digits = 6, decimal_places = 2) to store numbers with two decimal places.

5
  • model fields have decimal_places=2 and max_digits=10 already so something is rounding any value which is .00 Oct 3, 2012 at 11:04
  • 1
    Then probably there could be something wrong in the forms. Try printing data in clean_myfield. Check whether it is returning the value with two decimals.
    – arulmr
    Oct 3, 2012 at 11:12
  • Exactly. I always make stupid mistakes. So data = self.cleandecimal(data) in clean_myfield does it properly! But the model doesn't store the .00 decimal still. And if I directly edit the field in the db then it removes the .00 as well. this is a bigger problem Oct 3, 2012 at 11:23
  • Have you tried defining your cleandecimal function outside the form class? I tried with the same coding as yours. Everything is working fine for me. The only difference is I defined the cleandecimal function outside form class.
    – arulmr
    Oct 3, 2012 at 12:05
  • @miki725 pointed out that the problem is in the db and not Django, so his answer solves my problems. thanks though Oct 3, 2012 at 12:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.