Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/google-app-engine/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python“AttributeError:'unicode'对象没有属性'has_key'是什么意思?”_Python_Google App Engine - Fatal编程技术网

Python“AttributeError:'unicode'对象没有属性'has_key'是什么意思?”

Python“AttributeError:'unicode'对象没有属性'has_key'是什么意思?”,python,google-app-engine,Python,Google App Engine,我想问AttributeError是什么意思:“unicode”对象没有属性“has_key” 以下是完整的堆栈跟踪: 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 "

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

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'
让我再描述一下情况:

首先,我创建了models.py,其中包含CMSRequest的db.Model,它有一个引用CMSCountry类的属性country

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

class CMSCountry(db.Model):
  name = db.StringProperty(required=True)
然后,我创建了一个bulkloader类来将数据导入CMSContry

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

def post(self):
  postedcountry = self.request.get('country')
  cmsRequest = models.CMSRequest(postedcountry)
也许我已经找到了问题的解决方案,这是因为我没有将CMSContry发布的密钥转换回保存到CMSRequest

谢谢大家

在这一行中:

if value is not None and not value.has_key():
值是一个unicode字符串。看起来代码希望它是db.Model

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


是否将字符串传递给需要db.Model的GAE API?

如果阅读回溯,它将准确地告诉您发生了什么:

    if value is not None and not value.has_key():
AttributeError: 'unicode' object has no attribute 'has_key'

这说明您使用的值变量没有has_key属性。而且,它告诉您的是,您的值变量不是一个字典,就像您期望的那样……相反,它是一个unicode对象,基本上是一个字符串。

注意:通常在Python字典和类似字典的类中映射类型。。。例如各种类型的dbm索引文件和一些DBMS/ORM接口。。。将实现一个has_键方法

当您希望有某种字典或其他映射对象引用时,不知何故,您已经在该语句中加入了一个Unicode字符串对象

一般来说,AttributeError意味着您已经把对象绑定和变量分配搞混了。您已经为某些对象指定了一个名称,而不是您想要的类型。或者有时它意味着你有一个像.haskey而不是has_key这样的打字错误。。。等等

顺便说一句:has_键的使用有点过时了。通常,最好使用Python-in操作符测试容器,该操作符隐式调用_-contains _-,并且可以处理列表、字符串和其他序列以及映射类型

此外,value.has_键将引发错误,即使value是字典,因为.has_键方法需要参数

在您的代码中,如果postedcountry不是None,我会显式测试:。。。或者我会为您的.get提供postedcountry的可选默认值


您希望如何处理请求没有postedcountry的情况?您想假设它是从某个特定的默认值发布的吗?强制重定向到要求用户为该表单元素提供值的某个页面?警告西班牙宗教法庭?

您正试图将字符串设置为ReferenceProperty。CMSContry的“country”字段是db.ReferenceProperty,它接受db.Key或CMSContry对象,但您正试图将其设置为字符串

你应该这样做:

def post(self):
  postedcountry = self.request.get('country')
  country = models.CMSCountry.all().filter('name =', postedcountry)
  if not country:
    # Couldn't find the country
  else:
    cmsRequest = models.CMSRequest(country=country)

但是,请处理这个毫无帮助的错误消息。

您的问题是postedcountry是一个字符串,而不是一个country对象。从self.request.get检索的值是浏览器传递的变量的字符串值

您需要使用一些GQL查找country对象。具体的操作方式取决于HTML表单的国家字段返回的内容,对象键?,国家名称

def post(self):
  postedcountry = self.request.get('country')

  # <-------- Need to look up a country here !!!!!

  cmsRequest = models.CMSRequest(country=postedcountry)

dictionary对象的has_key方法在3.0中被弃用-请改用in表达式。如果您使用的是3.x中的旧库,则必须对代码进行更改以适应它。

@Hoang:看起来您需要类似于cmsRequest=models.CMSRequestcountry=models.CMSCountrypostedcountry的内容。我不知道这是否正是从字符串获取CMSCountry的正确方法,但您明白我的意思了。谢谢Richie,根据您的建议,我对代码进行了如下修改:def postself:postedcountry=self.request.get'country'country=models.cmscontry.getpostedcountry cmsRequest=models.CMSRequestcountry应该在选项的html中填充国家的密钥,并且postedcountry将具有cmscontry的密钥。