Python what does it mean "AttributeError: 'unicode' object has no attribute 'has_key' "

google-app-engine, python

Solution

In this line:

if value is not None and not value.has_key():

`value` is a unicode string. It looks like the code is expecting it to be a `db.Model`,

(From what I can see, `has_key` is a method of `db.Model`, as well as a method of Python dictionaries, but this must be the `db.Model` one because it's being called with no arguments.)

Are you passing a string to a GAE API that expects a `db.Model`?

Problem

I would like to ask what does it mean "AttributeError: 'unicode' object has no attribute 'has_key'" Here is the full stack trace: ``` Traceback (most recent call last): File "D:\Projects\GoogleAppEngine\google_appengine\google\appengine\ext\webapp\__init__.py", line 509, in __call__ handler.post(*groups) File "D:\Projects\workspace\foo\src\homepage.py", line 71, in post country=postedcountry File "D:\Projects\GoogleAppEngine\google_appengine\google\appengine\ext\db\__init__.py", line 656, in __init__ prop.__set__(self, value) File "D:\Projects\GoogleAppEngine\google_appengine\google\appengine\ext\db\__init__.py", line 2712, in __set__ value = self.validate(value) File "D:\Projects\GoogleAppEngine\google_appengine\google\appengine\ext\db\__init__.py", line 2742, in validate if value is not None and not value.has_key(): AttributeError: 'unicode' object has no attribute 'has_key' ``` Let me describe a little bit more about the situation: First I created the models.py that has the db.Model for CMSRequest which has an attribute country that reference to the CMSCountry class ``` class CMSRequest(db.Model): country = db.ReferenceProperty(CMSCountry, required=True) class CMSCountry(db.Model): name = db.StringProperty(required=True) ``` Then, I created a bulkloader class to import the data into the CMSCountry In the form, user can select the country from the drop down list box, the results are posted back and save to the CMSRequest object ``` def post(self): postedcountry = self.request.get('country') cmsRequest = models.CMSRequest(postedcountry) ``` Maybe I have found the solution to my question, it is because I have not converted the posted key of CMSCountry back to save to the CMSRequest. Thank you everyone!

Original source