I am making a small API server in Django. For one of my fields, I am using a decimal field which is responsible for representing $ dollar values, so I would like to have 2 decimal places at all times. But I'm struggling to have the exact format.
I'm using rest_framework and my base level response is encoded as a string with the following serializer is:
price = serializers.DecimalField(max_digits=10, decimal_places=2, required=True)
There is an option to turn off string coercion with "COERCE_DECIMAL_TO_STRING": False in settings.py, but then I face a problem that Decimal is not JSON serializable, and have to use FloatField where I cannot specify a format and end up with only 1 decimal place:
price = serializers.FloatField(required=True)
I searched for setting up FloatField format on the serializer but did not find any related option. I believe I also tried some other serializers like this one and did not achieve the goal, which is:
How do I enforce 2 decimal places on API response in Django?