0
我試圖從數據庫中提取一些數據,並使data['datetime']
JSON可序列化以將其從Django傳遞到Highcharts。我認爲我的方法可行,但看起來不行。我現在得到的錯誤是Type not serializable
,因爲它沒有從函數json_serial傳遞條件。我做錯了什麼,直到現在我還以爲dict ['key']會傳遞這樣的邏輯參數?從數據庫中提取數據並將其設置爲可序列化的JSON
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