I have a Django app as the backend a React app as the frontend. Most of my Django fields related to prices/monetary values were FloatField
. It worked with the frontend: when receiving objects from the backend I could see that those fields were number
s and I could make calculations with them. But I've recently changed from FloatField
to DecimalField
, and now on the frontend the prices are assumed as string
.
Why is that happening and what is the best approach to deal with this? Has anyone experienced something similar?
Asked
Active
Viewed 471 times
0

Sofia
- 445
- 4
- 17
-
1https://stackoverflow.com/a/49770815/8770336 – lucutzu33 Jun 24 '21 at 21:13
-
@EneP Thank you – Sofia Jun 24 '21 at 22:01
1 Answers
2
DecimalField
also takes an optional argument, coerce_to_string
. If set to True
the representation will be output as a string. If set to False
the representation will be left as a Decimal instance and the final representation will be determined by the renderer.
If unset, this will default to the same value as the COERCE_DECIMAL_TO_STRING
setting, which is True
unless set otherwise.
As you can see the default value of COERCE_DECIMAL_TO_STRING
is True, if you want to change it you can do this:
# settings.py
REST_FRAMEWORK = {
'COERCE_DECIMAL_TO_STRING': False,
...
}

Mojtaba Arezoomand
- 2,140
- 8
- 23