Nehax
Nehax

Reputation: 49

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. I'm trying to save the string that is inside temp

 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 !

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 !

Upvotes: 0

Views: 1277

Answers (2)

user3778137
user3778137

Reputation:

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)

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.

Upvotes: 1

LeoE
LeoE

Reputation: 2083

You are trying to use a dict as a dict-key, to cite your comment: "is a dict for some reason". Dicts are not hashable and therefore cannot be used as keys. Maybe extract the data from the dict and use this as key?

Upvotes: 1

Related Questions