简体   繁体   English

类型错误:不可散列类型:'dict' Python/Flask

[英]TypeError: unhashable type: 'dict' Python/Flask

So, I'm working on a little program that is supposed to send values to a firestore database , almost everything is working fine, but I get an error from this part of the code.所以,我正在开发一个应该将值发送到firestore database的小程序,几乎一切正常,但是我从这部分代码中收到错误消息。 I'm trying to save the string that is inside temp我正在尝试保存temp内的string

 if block == "ITEMS":
        champs = form.areaItems.data #Get the user input text field from the WTForm (it's a dict for whatever reason)
        itemsChamps = ItemsChamps(champs.values()) #Stock the dict value inside itemsChamps 
        temp = next(iter(itemsChamps.name)) #Get the 1st value from itemsChamps (I only want the 1st value)
        data = {
            "items": {
                champs: {
                    "string": temp
                }
            }
        }

Here is the error :这是错误:

File "C:\[..]\flaskblog\routes.py", line 63, in ajouter

"string": temp

TypeError: unhashable type: 'dict'

My code may look a bit """confusing""", I'm a newbie, sorry for that !我的代码可能看起来有点“”“令人困惑”“”,我是新手,抱歉!

Edit 1 : It work now !编辑 1:它现在工作!

I feel so dumb right now, I was confused a bit by all the code I wrote, there was a few mistake :我现在感觉很傻,我写的所有代码都让我有点困惑,有几个错误:

        if block == "ITEMS":
        champs = form.itemsFields.data #I was using the wrong form field...
        itemsChamps = ItemsChamps(form.areaItems.data.values()) #I'm now getting all the value from the right field
        temp = next(iter(itemsChamps.name)) #Didn't touch this, it work
        data = {
            "items": {
                champs: {
                    "string": temp
                }
            }
        }

Thanks you for giving me a little of your time !谢谢你给我一点时间!

You are trying to use a dict as a dict-key, to cite your comment: "is a dict for some reason".您正在尝试使用 dict 作为 dict-key,引用您的评论:“出于某种原因是 dict”。 Dicts are not hashable and therefore cannot be used as keys.字典不可散列,因此不能用作键。 Maybe extract the data from the dict and use this as key?也许从字典中提取数据并将其用作密钥?

The problem is with this piece of code.问题出在这段代码上。 champs is a dictionary, and you are using it as a key, dict key has to be a str, int, float (in general something that can be hashed and not a dictionary) champs是一个字典,你用它作为键,dict 键必须是 str、int、float(通常可以散列的东西而不是字典)

data = {
            "items": {
                champs: {
                    "string": temp
                }
            }
        }

If champs["user_input"] is the data you are interested in you can change champs to champs["user_input"] to solve this.如果champs["user_input"]是您感兴趣的数据,您可以将champs更改为champs["user_input"]来解决这个问题。

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

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