Django tastypie serialize DecimalField as json strings instead of numbers
I have an application django
with tastypie
. One of the models of my application has DecimalField
. When I receive a response from the API in the format JSON
, all decimal fields are displayed as, strings
instead of numbers
:
For example, I get:
objects: [
{
id: "1",
my_decimal_field: "84.54"
}
instead
objects: [
{
id: "1"
my_decimal_field: 84.54
}
This also happens with the id field.
¿Any thoughts?
source to share
In JavaScript, JSON decodes double precision floating point format , resulting in loss of precision. Decimal
objects are coded before string
to maintain accuracy.
If you want to encode the JSON number format, you can use FloatField
.
source to share