Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DecimalField serialised as string #508

Closed
0atman opened this issue Dec 14, 2012 · 13 comments
Closed

DecimalField serialised as string #508

0atman opened this issue Dec 14, 2012 · 13 comments

Comments

@0atman
Copy link

0atman commented Dec 14, 2012

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

I don't think this is correct:

{
    "int field" : 1,
    "decimal_field": "0.00"
}
@0atman
Copy link
Author

0atman commented Dec 14, 2012

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

@0atman
Copy link
Author

0atman commented Dec 14, 2012

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

@tomchristie
Copy link
Member

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.

@0atman
Copy link
Author

0atman commented Dec 14, 2012

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

@tomchristie
Copy link
Member

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)

@0atman
Copy link
Author

0atman commented Dec 15, 2012

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

@tomchristie
Copy link
Member

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.

@lorinkoz
Copy link

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?

@tomchristie
Copy link
Member

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
}

@maryokhin
Copy link
Contributor

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)?

@tomchristie
Copy link
Member

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.

@lorinkoz
Copy link

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

@tomchristie
Copy link
Member

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

Yes. It'll be coming shortly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants