django-gcharts
django-gcharts copied to clipboard
Decimal numbers are not allowed
If the query containing the data to be plotted includes a decimal.Decimal field, the library complains that Decimal data-type is not handled:
DataTableException at /
Wrong type <class 'decimal.Decimal'> when expected number
The stack trace report that the exception is raised in /home/spegni/.virtualenvs/oa/local/lib/python2.7/site-packages/gcharts/contrib/gviz_api.py in CoerceValue
(line: 244) with the following lines:
...
elif value_type == "number":
if isinstance(value, (int, long, float)):
return value
raise DataTableException("Wrong type %s when expected number" % t_value)
At the moment my patch is very simple:
...
elif value_type == "number":
if isinstance(value, (int, long, float)):
return value
elif isinstance(value, decimal.Decimal):
return float(value)
raise DataTableException("Wrong type %s when expected number" % t_value)
Automatic casting to float
seems reasonable to me. I don't know if it's enough, I'm still testing it.
Hi, Thanks for the report. Unfortunately, this library has received less love than deserved over the past years. As you have discovered, it relies on the gviz_api library which is maintained by Google. There is an open issue 20 for the problem you describe, but I don't think they accept contributions.
I have mixed feelings about patching the gviz_api
library even though it is included, since it might unforeseen have side effects.
A possible fix can be that instead of patching the gviz_api
, we'll check for Decimal
fields somewhere around here, and cast it to float.
Uhm, I see your idea of handlign it in the values
or values_list
. At the moment my feelings are that being a matter of the way google api handles the data type (i.e. it does not handle it), it makes more sense to me to deal with it in the "driver" where you already do some conversions and pre-processing of data before sending it to the google library. It seems to me that it is less concerned with (or depending from) the queryset ...
Yeah, well my main point was to deal with it before it was sent to the gviz_api
=)
You're probably right that values
or values_list
might not be the best place to do it, but you got the point!
Feel free to submit a pull request if you got a good idea on how to do it. I'm a bit busy with other projects these days so I don't really have time to do it at the moment.