我有一本大字典,里面有50多个项目。我正在尝试以JSON形式返回此字典。
该代码是:-
return HttpResponse(json.dumps(responseData, indent = 0, default=str), content_type="application/json")
字典中有一些日期字段,因此我提到了default = str可以将它们转换为字符串。
当我打印json.dumps时,我确实看到json中存在正确的值。但是,当我将其放入HttpResponse时,出现以下错误:-
要解包的值太多(预期为2)
有人可以告诉我为什么会发生这种情况以及解决方法是什么吗?
编辑:-回溯
Internal Server Error: /ScApp2/home/App20/Details/
Traceback (most recent call last):
File "\Python\Python37\lib\site-packages\django\core\handlers\exception.py", line 35, in inner
response = get_response(request)
File "\Python\Python37\lib\site-packages\django\core\handlers\base.py", line 128, in _get_response
response = self.process_exception_by_middleware(e, request)
File "\Python\Python37\lib\site-packages\django\core\handlers\base.py", line 126, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "\PythonWorkspace\ScApp\ScApp2\views.py", line 88, in ModelDetails
cmsPointScore = PspstCmspointscore.objects.filter(model_id)
File "\Python\Python37\lib\site-packages\django\db\models\manager.py", line 82, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "\Python\Python37\lib\site-packages\django\db\models\query.py", line 836, in filter
return self._filter_or_exclude(False, *args, **kwargs)
File "\Python\Python37\lib\site-packages\django\db\models\query.py", line 854, in _filter_or_exclude
clone.query.add_q(Q(*args, **kwargs))
File "\Python\Python37\lib\site-packages\django\db\models\sql\query.py", line 1253, in add_q
clause, _ = self._add_q(q_object, self.used_aliases)
File "\Python\Python37\lib\site-packages\django\db\models\sql\query.py", line 1277, in _add_q
split_subq=split_subq,
File "\Python\Python37\lib\site-packages\django\db\models\sql\query.py", line 1150, in build_filter
arg, value = filter_expr
ValueError: too many values to unpack (expected 2)
答案 0 :(得分:3)
错误出在您的查询中(这就是为什么我要求回溯):
cmsPointScore = PspstCmspointscore.objects.filter(model_id)
您应该将字段名称添加到过滤器表达式中,例如:
cmsPointScore = PspstCmspointscore.objects.filter(model_id=model_id)