I use Django and DRF,I have many decimal fields in models, like
amount = models.DecimalField(max_digits=16, decimal_places=4)
I want format output Two decimal places ,in every serializers field can do this
amount = serializers.DecimalField(max_digits=16, decimal_places=2)
There's an easyer way?
In DRF there is a way to override only options but not the entire field:
class MySerializer(serializers.ModelSerializer):
class Meta:
model = MyModel
fields = ('amount',)
extra_kwargs = {
'amount': {'max_digits': 16, 'decimal_places': 2}
}
Or if you have many same decimal fields you should use your own custom DecimalField with predefined common options.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With