[Fixed]-Django chartit causes Mysql date json serialization error

1👍

So I managed to solve the problem in stupid way but some say that if it seems stupid but it works it is not stupid.

The solution was to modify simplejson library, more specifically it was ‘simplejson/encoder.py’ file. I’ve changed the ‘default’ method which is normally supposed to be overriden by more complex types from:

def default(self, o):
    raise TypeError(repr(o) + " is not JSON serializable")

to:

def default(self, o):
    if isinstance(o, date):
        return o.strftime('%Y-%m-%d')
    raise TypeError(repr(o) + " is not JSON serializable")

I also had to add ‘from datetime import date’ on the top.
I was trying to do it otherwise but only this solution worked. I hope it will help someone in the future, and I also hope someone suggests better solution.

👤asdfgh

0👍

You got a Not JSON Serializable Object in this case a date comming from the field date in DailyStateArchive, you will need pre-process that value to send it to the view. You could try this:

myData = DailyStateArchive.objects.filter(company_name='06MAGNA').values()

for data in myData:
    data['date'] = data['date'].strftime('%Y-%m-%d')

ds = DataPool(
  series=
    [{'options': {
        'source': myData },
      'terms': [
          'date',
          'price'
        ]},

    ])

chart = Chart(
        datasource = ds, 
        series_options = 
          [{'options':{
              'type': 'line',
              'stacking': False},
            'terms':{
              'date' : [
                'price'
                ]
              }}],
        chart_options = 
          {'title': {
               'text': 'My Plot'},
           'xAxis': {
                'title': {
                   'text': 'Date'}}})
👤Gocht

Leave a comment