Google AppEngine告诉我我的int不是int

|| 代码的相关部分:
pk = int(pk)                              
logging.info(\'pk: %r :: %s\', pk, type(pk))
instance = models.Model.get_by_id(int(pk))       
上面的日志消息的输出
pk: 757347 :: <type \'int\'>
堆栈跟踪:
Traceback (most recent call last):
  File \"/base/python_runtime/python_lib/versions/1/google/appengine/ext/webapp/__init__.py\", line 634, in __call__
    handler.get(*groups)
  File \"/base/data/home/apps/<myapp>/<version>/scrape.py\", line 61, in get
    instance = models.Model.get_by_id(int(pk))
  File \"/base/python_runtime/python_lib/versions/1/google/appengine/ext/db/__init__.py\", line 1212, in get_by_id
    return get(keys[0], config=config)
  File \"/base/python_runtime/python_lib/versions/1/google/appengine/ext/db/__init__.py\", line 1434, in get
    model = cls1.from_entity(entity)
  File \"/base/python_runtime/python_lib/versions/1/google/appengine/ext/db/__init__.py\", line 1350, in from_entity
    instance = cls(None, _from_entity=True, **entity_values)
  File \"/base/python_runtime/python_lib/versions/1/google/appengine/ext/db/__init__.py\", line 890, in __init__
    prop.__set__(self, value)
  File \"/base/python_runtime/python_lib/versions/1/google/appengine/ext/db/__init__.py\", line 593, in __set__
    value = self.validate(value)
  File \"/base/python_runtime/python_lib/versions/1/google/appengine/ext/db/__init__.py\", line 2967, in validate
    % (self.name, type(value).__name__))
BadValueError: Property pk must be an int or long, not a unicode
有人知道我在这里做错了吗? 注意:从代码的最后一行删除ѭ3并没有区别(这是第一个版本)。 同样,代码在
dev_appserver.py
上运行也没有问题。     
已邀请:

bab

您的模型是否具有属性\'pk \',该属性现在是IntegerProperty(),但以前是StringProperty(),并且ID为757347的实体已与模型的旧版本一起保存?     
为您的pk IntegerProperty创建一个自定义验证器。 我认为@ saxon-druce对失败有正确的想法。 您正在从数据存储中接收实体,并且from_entity函数将来自实体的数据应用于db.Model的初始化程序。 验证电话来自
google/appengine/ext/db/__init__.py
从SDK
class IntegerProperty(Property):
  \"\"\"An integer property.\"\"\"

  def validate(self, value):
    \"\"\"Validate integer property.

    Returns:
      A valid value.

    Raises:
      BadValueError if value is not an integer or long instance.
    \"\"\"
    value = super(IntegerProperty, self).validate(value)
    if value is None:
      return value





    if not isinstance(value, (int, long)) or isinstance(value, bool):
      raise BadValueError(\'Property %s must be an int or long, not a %s\'
                          % (self.name, type(value).__name__))
    if value < -0x8000000000000000 or value > 0x7fffffffffffffff:
      raise BadValueError(\'Property %s must fit in 64 bits\' % self.name)
    return value

  data_type = int
创建自己的琐碎验证器,尝试将字符串解析为int。 最终,您可能希望在所有这些实体上应用一个映射器,以使它们全部进入当前架构。 验证者在ѭ7内被称为 因此该值应准备好在适当的时候用作int值。 验证器示例
def str_int_validator(value):
    if isinstance(value, basestring):
        if value.isdigit():
            return int(value)
        else:
            raise db.BadValueError(\"Property expected str or int got %r\" % value)
    else:
        return value

class Foo(db.Model):

    pk = db.IntegerProperty(validator=str_int_validator)
该代码尚未经过测试。     
删除一个实体/表中的所有元素,然后尝试通过csv / bulkloader上传新值后,出现了相同的错误消息。 解决方法是添加以下行
import_transform: transform.none_if_empty(int)
批量加载程序的yaml文件中的属性定义。     

要回复问题请先登录注册