-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
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
Comments
Same problem with |
Hmm, I might have been wrong when I said |
Decimals need to be serialized as strings in JSON, since float representation would lose precision.
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. |
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. On 15 Dec 2012, at 12:41, Tristram Oaten [email protected] wrote:
|
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.
|
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? |
Yes. It'll be coming shortly. |
Ints are serialised correctly, but
DecimalField
is serialsed to String.I don't think this is correct:
The text was updated successfully, but these errors were encountered: