无法从Google App Engine获取记录

时间:2012-05-04 14:00:08

标签: google-app-engine gql

我收集了数据,其数据不是特别干净,而且已经批量上传到DataStore。但是,当我尝试简单地遍历所有记录时,我遇到了以下问题。我现在不太关心验证,因为我想要做的就是执行批量操作,但GAE似乎不让我甚至循环遍历数据记录。我想深究这一点。据我所知,所有记录都包含该国家的数据,我可以切换验证,但有人可以解释为什么会发生这种情况并且GAE敏感。感谢

result = Company.all()
        my_count = result.count()
        if result:
            for r in result:
                self.response.out.write("hello")

数据模型具有以下属性:

class Company(db.Model):
    companyurl = db.LinkProperty(required=True)
    companyname = db.StringProperty(required=True)
    companydesc = db.TextProperty(required=True)
    companyaddress = db.PostalAddressProperty(required=False)
    companypostcode = db.StringProperty(required=False)
    companyemail = db.EmailProperty(required=True)
    companycountry = db.StringProperty(required=True)

错误消息在

下面
Traceback (most recent call last):
  File "/base/python_runtime/python_lib/versions/1/google/appengine/ext/webapp/_webapp25.py", line 701, in __call__
    handler.get(*groups)
  File "/base/data/home/apps/XXX/1.358667163009710608/showcompanies.py", line 99, in get
    for r in result:
  File "/base/python_runtime/python_lib/versions/1/google/appengine/ext/db/__init__.py", line 2312, in next
    return self.__model_class.from_entity(self.__iterator.next())
  File "/base/python_runtime/python_lib/versions/1/google/appengine/ext/db/__init__.py", line 1441, in from_entity
    return cls(None, _from_entity=entity, **entity_values)
  File "/base/python_runtime/python_lib/versions/1/google/appengine/ext/db/__init__.py", line 973, in __init__
    prop.__set__(self, value)
  File "/base/python_runtime/python_lib/versions/1/google/appengine/ext/db/__init__.py", line 613, in __set__
    value = self.validate(value)
  File "/base/python_runtime/python_lib/versions/1/google/appengine/ext/db/__init__.py", line 2815, in validate
    value = super(StringProperty, self).validate(value)
  File "/base/python_runtime/python_lib/versions/1/google/appengine/ext/db/__init__.py", line 640, in validate
    raise BadValueError('Property %s is required' % self.name)
BadValueError: Property companycountry is required

1 个答案:

答案 0 :(得分:1)

如果您希望在自己的脚本中运行批量处理,则可以构建Company类的修改版本而无需验证。由于db.Model类只是基于类名称的数据存储区的包装器,因此您可以在代码的不同部分使用不同的行为来创建不同的类。

所以你可能有一个带有:

的model.py文件
class Company(db.Model):
  companyurl = db.LinkProperty(required=True)
  # ...
  companycountry = db.StringProperty(required=True)

# Normal operations go here

另一个bulk_process.py文件:

class Company(db.Model):
  companyurl = db.LinkProperty()
  # ...
  companycountry = db.StringProperty()

result = Company.all()
my_count = result.count()
if result:
  for r in result:
    self.response.out.write("hello")

因为第二个模型类缺少验证,所以应该运行得很好。而且,由于代码在逻辑上是分开的,因此您不必担心在其余代码中删除验证时会产生无意的副作用。只需确保您的批量过程不会在未经验证的情况下意外写回数据(除非您对此感到满意)。