I'm trying to pull some data from database and make data['datetime']
JSON serializable to pass it from Django to Highcharts. I thought my method would work, but it appears it does not. The error I'm getting at the moment is Type not serializable
since it's not passing the condition from function json_serial. What am I doing wrong, until now I thought that dict['key'] would pass such logic parameter?
我正在尝试从数据库中提取一些数据并使数据['datetime'] JSON可序列化以将其从Django传递给Highcharts。我认为我的方法可行,但似乎没有。我现在得到的错误是Type不可序列化,因为它没有从函数json_serial传递条件。我做错了什么,直到现在我还以为dict ['key']会传递这样的逻辑参数?
from .models import Heater
import json
from datetime import datetime
def json_serial(obj):
"""JSON serializer for objects not serializable by default json code"""
if isinstance(obj, datetime):
serial = obj.isoformat()
return serial
raise TypeError ("Type not serializable")
class ChartData(object):
def check_heater_data(self):
data = {'datetime': [], 'temp_max': [],
'temp_min': []}
valves = Heater.objects.all()
for unit in valves:
data['temp_max'].append(unit.temp_max)
data['temp_min'].append(unit.temp_min)
data['datetime'] = json_serial(data['datetime'].append(unit.datetime))
jsonstring = json.dumps(data)
jsondata = json.loads(jsonstring)
return jsondata
1 个解决方案
#1
0
Because you are passing list
object to json_serial
function.
因为您将列表对象传递给json_serial函数。
Here: json_serial(data['datetime'].append(unit.datetime))
, which is obviously not an instance of datetime
.
这里:json_serial(data ['datetime']。append(unit.datetime)),它显然不是datetime的实例。
Try to change it to something like this: data['datetime'].append(json_serial(unit.datetime))
.
尝试将其更改为:data ['datetime']。append(json_serial(unit.datetime))。
#1
0
Because you are passing list
object to json_serial
function.
因为您将列表对象传递给json_serial函数。
Here: json_serial(data['datetime'].append(unit.datetime))
, which is obviously not an instance of datetime
.
这里:json_serial(data ['datetime']。append(unit.datetime)),它显然不是datetime的实例。
Try to change it to something like this: data['datetime'].append(json_serial(unit.datetime))
.
尝试将其更改为:data ['datetime']。append(json_serial(unit.datetime))。