PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, August 12, 2022

[FIXED] How do I get Django Rest Framework to round decimals to the maximum precision?

 August 12, 2022     decimal, django-rest-framework, rounding     No comments   

Issue

I have a Django Rest Framework serializer with a DecimalField

serializers.DecimalField(max_digits=9, decimal_places=6)

Now if I try to deserialize data that contains a decimal with a higher precision (i.e. 50.1234567) the serializer raises a ValidationError:

"Ensure that there are no more than 6 decimal places."

This even happens if the last digit is 0. Is it possible to make the serializer round the given value to the maximum precision (i.e. 50.1234567 to 50.123457)? And if so how?


Solution

After coercing the input to a Decimal the DecimalField validates the precision of the value in the aptly named, but undocumented, validate_precision method. So to disable this validation one can override this method and simply return the input value:

class RoundingDecimalField(serializers.DecimalField):
    def validate_precision(self, value):
        return value

It turns out that doing this is enough to get the desired rounding behaviour.

After calling validate_precision the DecimalField calls quantize which will "Quantize the decimal value to the configured precision" (from the docstring).

The rounding mode for this quantisation process is controlled by the current active decimal context.

If a specific rounding mode is desired one can use the (again undocumented) drf_braces.fields.custom.RoundedDecimalField field from django-rest-framework-braces. This field takes an optional rounding argument where one can specify the desired rounding mode.



Answered By - jaap3
Answer Checked By - Pedro (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing