我正在开发django-rest-framework API,在其中我对VectorWise数据库进行一些SQL查询和计算,并创建带有一些输入参数的响应。
起初我有这样的结构:
response = {'Date': input_date,
'Date comparable': date_comparable,
'CA': {
'CA TTC (€)': ca_ttc_n,
'Rang magasin': rank,
'progression (%)': progression_percentage
},
'Nb art / pass caisse': {
'nombre': nb_art_pass_caiss,
'progression (%)': nb_art_pass_caiss_prog
},
'Panier': {
'(€)': panier,
'progression (%)': prog_panier}
}
哪个工人还好,并给我回复。
然后,我试图像这样改善响应的代码结构和可读性:
response = {'Date': input_date,
'Comparable date': date_comparable,
'Indicators': {
{"Name": "sales",
"Value": ca_ttc_n,
"Unit": currency,
"Rank": rank},
{"Name": "sales_progression",
"Value": progression_percentage,
"Unit": percentage,
"Rank": rank},
{"Name": "customer_items_number",
"Value": nb_art_pass_caiss,
"Unit": "Units",
"Rank": rank},
{"Name": "customer_items_number_progression",
"Value": nb_art_pass_caiss_prog,
"Unit": percentage,
"Rank": rank},
{"Name": "basket",
"Value": panier,
"Unit": currency,
"Rank": rank}
}
另一方面,这会导致抛出TypeError错误
Traceback (most recent call last):
File "C:\ProgramData\Anaconda3\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
response = get_response(request)
File "C:\ProgramData\Anaconda3\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:\ProgramData\Anaconda3\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\ProgramData\Anaconda3\lib\site-packages\django\views\decorators\csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "C:\ProgramData\Anaconda3\lib\site-packages\django\views\generic\base.py", line 71, in view
return self.dispatch(request, *args, **kwargs)
File "C:\ProgramData\Anaconda3\lib\site-packages\rest_framework\views.py", line 495, in dispatch
response = self.handle_exception(exc)
File "C:\ProgramData\Anaconda3\lib\site-packages\rest_framework\views.py", line 455, in handle_exception
self.raise_uncaught_exception(exc)
File "C:\ProgramData\Anaconda3\lib\site-packages\rest_framework\views.py", line 466, in raise_uncaught_exception
raise exc
File "C:\ProgramData\Anaconda3\lib\site-packages\rest_framework\views.py", line 492, in dispatch
response = handler(request, *args, **kwargs)
File "C:\ProgramData\Anaconda3\lib\site-packages\rest_framework_extensions\cache\decorators.py", line 49, in inner
kwargs=kwargs,
File "C:\ProgramData\Anaconda3\lib\site-packages\rest_framework_extensions\cache\decorators.py", line 68, in process_cache_response
response = view_method(view_instance, request, *args, **kwargs)
File "C:\Users\10124077\Projects\dataplatform--web--api\wdc\views.py", line 484, in get
"Rank": rank}
TypeError: unhashable type: 'dict'
我尝试删除rank字段并进行一些操作以更好地理解该问题,但是由于它不断在响应正文的其他字段上引发TypeError,所以我没有设法做到!有帮助吗?
答案 0 :(得分:4)
问题在于:
'Indicators': {
{"Name": "sales",
"Value": ca_ttc_n,
"Unit": currency,
"Rank": rank},
{"Name": "sales_progression",
"Value": progression_percentage,
"Unit": percentage,
"Rank": rank},
{"Name": "customer_items_number",
"Value": nb_art_pass_caiss,
"Unit": "Units",
"Rank": rank},
{"Name": "customer_items_number_progression",
"Value": nb_art_pass_caiss_prog,
"Unit": percentage,
"Rank": rank},
{"Name": "basket",
"Value": panier,
"Unit": currency,
"Rank": rank}
}
...尝试创建字典的集。因此,您的代码基本上是这样做的:
{{"this": "is", "a": "dictionary"}, {"within": "a set"}}
之所以无法做到这一点,是因为只能构造一组可哈希对象,而dict
是不可哈希的。您可以了解“ hashable”的含义in the docs。不过,您可以使用词典列表:
"Indicators": [
{'Name': ..., 'Value': ...},
{'Name': ..., 'Value': ...},
...
]
答案 1 :(得分:1)
您缺少列表:
'Indicators': [
{"Name": "sales",
"Value": ca_ttc_n,
"Unit": currency,
"Rank": rank},
...
]
}
答案 2 :(得分:1)
这是因为您试图设置没有任何键值的字典指标(具有字典等不可散列的值)。
response = {'Date': input_date,
'Comparable date': date_comparable,
'Indicators': {
'key1':{"Name": "sales",
"Value": ca_ttc_n,
"Unit": currency,
"Rank": rank},
'key2':{"Name": "sales_progression",
"Value": progression_percentage,
"Unit": percentage,
"Rank": rank},
'key3':{"Name": "customer_items_number",
"Value": nb_art_pass_caiss,
"Unit": "Units",
"Rank": rank},
'key4':{"Name": "customer_items_number_progression",
"Value": nb_art_pass_caiss_prog,
"Unit": percentage,
"Rank": rank},
'key5':{"Name": "basket",
"Value": panier,
"Unit": currency,
"Rank": rank}
}
}
否则,如果不需要按键,请在指示器中列出一个列表。