Adds a new custom serializer field - InvenTreeDecimalField

- Fixes a number of problems with using FloatField
This commit is contained in:
Oliver 2021-11-18 22:17:58 +11:00
parent 8a879832ee
commit 35bd6ce927

View File

@ -296,3 +296,17 @@ class InvenTreeImageSerializerField(serializers.ImageField):
return None
return os.path.join(str(settings.MEDIA_URL), str(value))
class InvenTreeDecimalField(serializers.FloatField):
"""
Custom serializer for decimal fields. Solves the following issues:
- The normal DRF DecimalField renders values with trailing zeros
- Using a FloatField can result in rounding issues: https://code.djangoproject.com/ticket/30290
"""
def to_internal_value(self, data):
# Convert the value to a string, and then a decimal
return Decimal(str(data))