Python是什么意思“AttributeError:'unicode'对象没有属性'has_key'”。 [英] Python what does it mean "AttributeError: 'unicode' object has no attribute 'has_key' "

查看:246
本文介绍了Python是什么意思“AttributeError:'unicode'对象没有属性'has_key'”。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想问一下这是什么意思AttributeError:'unicode'对象没有属性'has_key'
这是完整的堆栈跟踪:

  Traceback(最近一次调用最后一次):
文件D:\Projects\GoogleAppEngine\google_appengine\google\appengine\ext\webapp\ __init__.py,第509行,在__call__
handler.post(* groups)
文件D:\Projects\workspace\foo\src\homepage.py,第71行,在后
country = postingcountry
文件D:\Projects\GoogleAppEngine\google_appengine\google\appengine\ext\db\__init __。py,行656,in __init__
道具.__设置__(self,value)
文件D:\Projects\GoogleAppEngine\google_appengine\google\appengine\ext\db\__init __。py,行2712,在__set__
value = self.validate(value)
文件D:\Projects\GoogleAppEngine\google_appengine\\如果值不是None而不是value.has_key():
AttributeError:'unicode'对象具有验证
的google \appengine\ext\db\__init __。py。,第2742行没有属性'has_key'

让我更详细地描述一下情况:




  • 首先,我创建了具有CMSRequest的db.Model的models.py,它具有引用CMSCountry类的属性country

      class CMSRequest(db.Model):
    country = db.ReferenceProperty(CMSCountry,required = True)

    class CMSCountry(db.Model):
    name = db.StringProperty(required = True)


    然后,我创建了一个批量加载器类来将数据导入CMSCountry


  • 在表单中,用户可以从drop中选择国家下拉列表框中,结果被回发并保存到CMSRequest对象中。

    pre $ def $($)
    postedcount ry = self.request.get('country')
    cmsRequest = models.CMSRequest(postedcountry)




也许我找到了解决我的问题的方法,这是因为我没有将CMSCountry的已发布密钥转换回保存到CMSRequest。



谢谢大家!

解决方案

在这一行中:

 如果value不是None而不是value.has_key():

是一个unicode字符串。它看起来像代码期望它是一个 db.Model



(从我所看到的, has_key db.Model 的一种方法,也是Python字典的一种方法,但这必须是 db.Model 一个,因为它被调用时没有参数。)



是否将字符串传递给GAE API期望 db.Model


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!

解决方案

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?

这篇关于Python是什么意思“AttributeError:'unicode'对象没有属性'has_key'”。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆