得到"期望的字符串,得到无" Model.get_by_id()上的错误

时间:2016-06-27 09:42:12

标签: python google-app-engine google-cloud-datastore

运行此代码时,使用相同的site_id

int_id= int(self.request.get("site_id"))
site_draft = SiteDraft.get_by_id(int_id)

我收到此错误:

INFO     2016-06-27 12:39:19,040 module.py:788] minisites: "GET /edit/5891733057437696 HTTP/1.1" 500 -
    return self.handle_exception(e, self.app.debug)
  File "C:\Program Files\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\lib\webapp2-2.3\webapp2.py", line 545, in dispatch
    return method(*args, **kwargs)
  File "C:\Users\Yuri\Documents\WiseStamp\Server\minisites\web\pages\edit_site.py", line 21, in get
    self.post(args)
  File "C:\Users\Yuri\Documents\WiseStamp\Server\minisites\web\pages\edit_site.py", line 44, in post
    site_draft = SiteDraft.get_by_id(int_id)
  File "C:\Program Files\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\google\appengine\ext\ndb\utils.py", line 160, in positional_wrapper
    return wrapped(*args, **kwds)
  File "C:\Program Files\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\google\appengine\ext\ndb\model.py", line 3602, in _get_by_id
    return cls._get_by_id_async(id, parent=parent, **ctx_options).get_result()
  File "C:\Program Files\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\google\appengine\ext\ndb\tasklets.py", line 378, in get_result
    self.check_success()
  File "C:\Program Files\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\google\appengine\ext\ndb\tasklets.py", line 425, in _help_tasklet_along
    value = gen.send(val)
  File "C:\Program Files\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\google\appengine\ext\ndb\context.py", line 765, in get
    pbs = entity._to_pb(set_key=False).SerializePartialToString()
  File "C:\Program Files\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\google\appengine\ext\ndb\model.py", line 3167, in _to_pb
    prop._serialize(self, pb, projection=self._projection)
  File "C:\Program Files\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\google\appengine\ext\ndb\model.py", line 1422, in _serialize
    values = self._get_base_value_unwrapped_as_list(entity)
  File "C:\Program Files\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\google\appengine\ext\ndb\model.py", line 1192, in _get_base_value_unwrapped_as_list
    wrapped = self._get_base_value(entity)
  File "C:\Program Files\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\google\appengine\ext\ndb\model.py", line 1180, in _get_base_value
    return self._apply_to_values(entity, self._opt_call_to_base_type)
  File "C:\Program Files\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\google\appengine\ext\ndb\model.py", line 1352, in _apply_to_values
    value[:] = map(function, value)
  File "C:\Program Files\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\google\appengine\ext\ndb\model.py", line 1234, in _opt_call_to_base_type
    value = _BaseValue(self._call_to_base_type(value))
  File "C:\Program Files\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\google\appengine\ext\ndb\model.py", line 1255, in _call_to_base_type
    return call(value)
  File "C:\Program Files\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\google\appengine\ext\ndb\model.py", line 1331, in call
    newvalue = method(self, value)
  File "C:\Program Files\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\google\appengine\ext\ndb\model.py", line 1781, in _validate
    (value,))
BadValueError: Expected string, got None

它曾经在一天前工作,但现在只有20%的时间工作(这也很奇怪 - 它似乎工作了一些时间)。甚至更奇怪的是我提供int作为site_id,但它告诉我它得到了None

请告知。

1 个答案:

答案 0 :(得分:5)

作为请求

详细查看堆栈跟踪并注意所执行的操作,

我们看到了

 _get_by_id
 get_result
 _help_tasklet_al
 get
 _to_pb
 _serialize
 _get_base_value
_get_base_value
_apply_to_values
_opt_call_to_base
_call_to_base_type
call
_validate


BadValueError: Expected string, got None

我们看到执行的路径在_get_by_id内,而且正在处理get_result。所以已经检索了一个实体。进一步向下看,我们看到代码实际上验证了检索到的值,并在使用BadValueError的_validate调用中失败:预期的字符串,得到无

如何在现有实体中获得错误值。通常情况下,这是您更改模型的结果

  • 添加了新的必需属性,但未更新现有实体。
  • 更改了现有属性的类型
  • 在属性中添加了验证程序,但未使现有实体合规。

更改模型时,考虑实体迁移非常重要。

干杯

Ť