Django-rest-framework: DecimalField serialised as string

Created on 14 Dec 2012  Â·  13Comments  Â·  Source: encode/django-rest-framework

Ints are serialised correctly, but DecimalField is serialsed to String.

I don't think this is correct:

{
    "int field" : 1,
    "decimal_field": "0.00"
}

Most helpful comment

3.0 gives you the option to serialize decimals as floats.

https://github.com/tomchristie/django-rest-framework/blob/version-3.0/docs/topics/3.0-announcement.md#decimals-as-strings-in-serializer-data

REST_FRAMEWORK = {
    'COERCE_DECIMAL_TO_STRING': False
}

All 13 comments

Same problem with FloatField. I've noticed there doesn't seem to be a DecimalField serializer.

Hmm, I might have been wrong when I said IntegerField was correct. It seems only ID is serialised to a JSON number.

Decimals need to be serialized as strings in JSON, since float representation would lose precision.
You end up with weird things like 0.1 -> 0.09999999999
Same approach taken in Django's fixtures.

Hmm, I might have been wrong when I said IntegerField was correct. It seems only ID is serialised to a JSON number.

If you can clarify feel free to open that as a separate issue. Not aware of any issue currently, but could be wrong.

Fixed-place decimals don't have the problem with precision, surely? That's why I'm using them in my application.

Fixed-place decimals don't have the problem with precision, surely?

They don't. Until they get converted to _binary_ floating point.

There's no such thing as a Decimal type in Javascript, so you need to use string reprs.

http://stackoverflow.com/questions/1960516/python-json-serialize-a-decimal-object

(And yeah I was a bit surprised the first time I realized it was so, too)

Lame! It looks like simplejson supports decimal exporting correctly, but that would add a dependency to DRF...

simplejson is bundled with Django.
Looking at this http://code.google.com/p/simplejson/issues/detail?id=34 seems like you can tweak the JSONRenderer to use use_decimal and things will work as you're hoping. It's likely you'll still lose precision on the other side of the wire, but still...

On 15 Dec 2012, at 12:41, Tristram Oaten [email protected] wrote:

Lame! It looks like simplejson supports decimal exporting correctly, but that would add a dependency to DRF...

—
Reply to this email directly or view it on GitHub.

Here's a problem with parsing decimals as string: If you're using AngularJs and bind a Django generated form with a DecimalField to an object coming from a DRF api, the initial object to form binding doesn't work, because the object property is a string and the field expects a numeric type. Any way to work this around?

3.0 gives you the option to serialize decimals as floats.

https://github.com/tomchristie/django-rest-framework/blob/version-3.0/docs/topics/3.0-announcement.md#decimals-as-strings-in-serializer-data

REST_FRAMEWORK = {
    'COERCE_DECIMAL_TO_STRING': False
}

By the way, we have the opposite need, to serialize IDs as strings, because Javascript doesn't support 64-bit integers. Is there a pretty way to do this (apart from matching *_id field names in renderer)?

Quick and dirty: use the renderer as you suggest. Fuller: override ModelSerializer, creating a new base class that gives you a string based relation field for relationships. The API for allowing users to make that kind of customization isn't really done yet tho.

Thanks for answering. Are you planning to make version 3.x available on PyPi anytime soon?

Are you planning to make version 3.x available on PyPi anytime soon?

Yes. It'll be coming shortly.

Was this page helpful?
0 / 5 - 0 ratings