【发布时间】:2019-06-03 16:15:43
【问题描述】:
我在 python 中使用 DateTime 值时遇到了一些问题。当我在以下代码中使用 session_start 时,我得到一个类型为 datetime 的 Object is not JSON serializable 错误
views.py
dataSourceBar = {}
dataSourceBar['chart'] = {
"caption": "Rainfall",
"subCaption": "Shown per date",
"xAxisName": "Session",
"yAxisName": "Rainfall in MM",
"theme": "candy"
}
dataSourceBar['data'] = []
objects_with_category_id_2 = dashboard_input.objects.filter(category_category_id=2)
for obj in objects_with_category_id_2:
data = {'label': obj.session_start,
'value': obj.input_input_value}
dataSourceBar['data'].append(data)
模型.py
class dashboard_input(models.Model):
session_start = models.DateTimeField(max_length=100)
追溯
Internal Server Error: /dashboard/
Traceback (most recent call last):
File "C:\Users\natas\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
response = get_response(request)
File "C:\Users\natas\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\handlers\base.py", line 126, in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:\Users\natas\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\handlers\base.py", line 124, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\var\www\VSDK\vsdk\dashboard\views.py", line 69, in Chart
return render(request, 'dash.html', {'output' : column2D.render(),'output2' : doughnut3d.render()})
File "C:\var\www\VSDK\vsdk\dashboard\fusioncharts.py", line 52, in render
self.readyJson = json.dumps(self.constructorOptions, ensure_ascii=False)
File "C:\Users\natas\AppData\Local\Programs\Python\Python37-32\lib\json\__init__.py", line 238, in dumps
**kw).encode(obj)
File "C:\Users\natas\AppData\Local\Programs\Python\Python37-32\lib\json\encoder.py", line 199, in encode
chunks = self.iterencode(o, _one_shot=True)
File "C:\Users\natas\AppData\Local\Programs\Python\Python37-32\lib\json\encoder.py", line 257, in iterencode
return _iterencode(o, 0)
File "C:\Users\natas\AppData\Local\Programs\Python\Python37-32\lib\json\encoder.py", line 179, in default
raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type datetime is not JSON serializable
我从数据库视图中检索值,该视图由用户填写其数据的其他表填充。
有人可以帮我解决这个问题吗?
【问题讨论】: