简体   繁体   English

API提供TypeError:不可哈希类型:'dict'

[英]API gives TypeError: unhashable type: 'dict'

I am developing a django-rest-framework API where I do some SQL querying and calculations on a VectorWise database and create a response with get to some input parameters. 我正在开发django-rest-framework API,在其中我对VectorWise数据库进行一些SQL查询和计算,并创建带有一些输入参数的响应。

I had this structure for my response at first: 一开始,我的响应结构如下:

    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}
                }

Which worker just fine and gives me back a response. 哪个工人很好,并给了我回复。

Then, I tried to improve the code structure and readability of the response like so: 然后,我尝试改善响应的代码结构和可读性,如下所示:

    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}
                }

This on the other hand keeps throwing errors of TypeError 另一方面,这会导致抛出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'

I tried removing the rank field and manupilating a bit to get a better understanding of the issue but I didn't manage to do it as it keeps raising TypeError on other fields of the response body ! 我尝试删除rank字段并进行一些操作以更好地理解该问题,但是由于它不断在响应正文的其他字段上引发TypeError,因此我没有设法做到这一点! Any help ? 有什么帮助吗?

The problem is that this: 问题是:

'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}
            }

...attempts to create a set of dictionaries. ...尝试创建一词典。 So, your code basically does this: 因此,您的代码基本上是这样做的:

{{"this": "is", "a": "dictionary"}, {"within": "a set"}}

This can't be done because one can construct sets of hashable objects only , and dict s are not hashable. 之所以无法做到这一点,是因为只能构造一组可哈希对象 ,而dict不可哈希。 You can read about what "hashable" means in the docs . 您可以在docs中了解“ hashable”的含义。 You could use a list of dictionaries, though: 但是,您可以使用词典列表:

"Indicators": [
    {'Name': ..., 'Value': ...},
    {'Name': ..., 'Value': ...},
    ...
]

You're missing a list: 您缺少列表:

            'Indicators': [
                {"Name": "sales",
                 "Value": ca_ttc_n,
                 "Unit": currency,
                 "Rank": rank},
                  ...
              ]
            }

Its cause you are trying to set a dictionary Indicators (with unhashable values like dictionary) without any keys value. 这是因为您试图设置没有任何键值的字典指示符 (具有字典等不可散列的值)。

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}
                }
}

else if you do not need keys, make Indicators a list. 否则,如果不需要按键,则将“ 指示器”列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM