Datastore error: BadValueError: Expected integer, got [0, 1, 2, 3]

Datastore error: BadValueError: Expected integer, got [0, 1, 2, 3]

其他人也报过类似的错误,但是给出的解决方案并没有解决我的问题。 For example there is a good answer here. link 中的答案提到了 ndb 如何从第一次使用变为以后使用,并表明存在问题,因为第一个 运行 在数据存储中产生了 None .我无法在我的 sdk 的数据存储中重现或看到发生的情况,但这可能是因为我是从交互式控制台在此处 运行ning 的。

我很确定我用 GAE 交互式控制台得到了一个初步的好 运行,但是从那以后每个 运行 都失败了,我的标题中有这个问题的错误。

我在下面的代码中留下了打印语句,因为它们显示了良好的结果,并向我保证错误发生在最后的 put() 中。

from google.appengine.ext import ndb

class Account(ndb.Model):

    week = ndb.IntegerProperty(repeated=True)
    weeksNS = ndb.IntegerProperty(repeated=True)
    weeksEW = ndb.IntegerProperty(repeated=True)

terry=Account(week=[],weeksNS=[],weeksEW=[])
terry_key=terry.put()
terry = terry_key.get()
print terry

for t in list(range(4)):     #just dummy input, but like real input
    terry.week.append(t)
print terry.week

region = 1                   #same error message for region = 0
if region :
    terry.weeksEW.append(terry.week)
else:
    terry.weeksNS.append(terry.week)

print 'EW'+str(terry.weeksEW)
print 'NS'+str(terry.weeksNS)
terry.week = []
print 'week'+str(terry.week)
terry.put()

我的代码的想法是首先逐步构建 terry.week 列表值,然后将整个列表存储到适当的区域,NS 或 EW。所以我正在寻找这个方案的解决方法。

该错误消息可能没有任何价值,但我在这里重现它。

Traceback (most recent call last):
  File "/Users/brian/google-cloud-sdk/platform/google_appengine/google/appengine/tools/devappserver2/python/runtime/request_handler.py", line 237, in handle_interactive_request
    exec(compiled_code, self._command_globals)
  File "<string>", line 55, in <module>
  File "/Users/brian/google-cloud-sdk/platform/google_appengine/google/appengine/ext/ndb/model.py", line 3458, in _put
    return self._put_async(**ctx_options).get_result()
  File "/Users/brian/google-cloud-sdk/platform/google_appengine/google/appengine/ext/ndb/tasklets.py", line 383, in get_result
    self.check_success()
  File "/Users/brian/google-cloud-sdk/platform/google_appengine/google/appengine/ext/ndb/tasklets.py", line 427, in _help_tasklet_along
    value = gen.throw(exc.__class__, exc, tb)
  File "/Users/brian/google-cloud-sdk/platform/google_appengine/google/appengine/ext/ndb/context.py", line 824, in put
    key = yield self._put_batcher.add(entity, options)
  File "/Users/brian/google-cloud-sdk/platform/google_appengine/google/appengine/ext/ndb/tasklets.py", line 430, in _help_tasklet_along
    value = gen.send(val)
  File "/Users/brian/google-cloud-sdk/platform/google_appengine/google/appengine/ext/ndb/context.py", line 358, in _put_tasklet
    keys = yield self._conn.async_put(options, datastore_entities)
  File "/Users/brian/google-cloud-sdk/platform/google_appengine/google/appengine/datastore/datastore_rpc.py", line 1858, in async_put
    pbs = [entity_to_pb(entity) for entity in entities]
  File "/Users/brian/google-cloud-sdk/platform/google_appengine/google/appengine/ext/ndb/model.py", line 697, in entity_to_pb
    pb = ent._to_pb()
  File "/Users/brian/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 "/Users/brian/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 "/Users/brian/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 "/Users/brian/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 "/Users/brian/google-cloud-sdk/platform/google_appengine/google/appengine/ext/ndb/model.py", line 1352, in _apply_to_values
    value[:] = map(function, value)
  File "/Users/brian/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 "/Users/brian/google-cloud-sdk/platform/google_appengine/google/appengine/ext/ndb/model.py", line 1255, in _call_to_base_type
    return call(value)
  File "/Users/brian/google-cloud-sdk/platform/google_appengine/google/appengine/ext/ndb/model.py", line 1331, in call
    newvalue = method(self, value)
  File "/Users/brian/google-cloud-sdk/platform/google_appengine/google/appengine/ext/ndb/model.py", line 1781, in _validate
    (value,))
BadValueError: Expected integer, got [0, 1, 2, 3]

我认为错误来自这些行:

terry.weeksEW.append(terry.week)
terry.weeksNS.append(terry.week)

您没有附加另一个整数;当需要整数时,您正在附加一个列表。

>>> aaa = [1,2,3]
>>> bbb = [4,5,6]
>>> aaa.append(bbb)
>>> aaa
[1, 2, 3, [4, 5, 6]]
>>> 

这未通过 ndb.IntegerProperty 测试。

尝试:

terry.weeksEW += terry.week
terry.weeksNS += terry.week

编辑: 要保存列表列表,请不要使用 IntegerProperty(),而是使用 JsonProperty()。更好的是,ndb 数据存储已被弃用,所以...我推荐 Firestore,它默认使用 JSON 个对象。至少使用 Cloud Datastore 或 Cloud NDB。