Python 将项目附加到ListProperty的应用程序引擎
我想我疯了,为什么下面的工作不起作用Python 将项目附加到ListProperty的应用程序引擎,python,google-app-engine,google-cloud-datastore,Python,Google App Engine,Google Cloud Datastore,我想我疯了,为什么下面的工作不起作用 class Parent(db.Model): childrenKeys = db.ListProperty(str,indexed=False,default=None) p = Parent.get_or_insert(key_name='somekey') p.childrenKeys = p.childrenKeys.append('newchildkey') p.put() 我得到这个错误: BadValueError: Propert
class Parent(db.Model):
childrenKeys = db.ListProperty(str,indexed=False,default=None)
p = Parent.get_or_insert(key_name='somekey')
p.childrenKeys = p.childrenKeys.append('newchildkey')
p.put()
我得到这个错误:
BadValueError: Property childrenKeys is required
医生说:
default是列表属性的默认值。如果没有,则为
默认为空列表。列表属性可以定义自定义属性
验证程序禁止空列表
因此,在我看来,我正在获取默认值(一个空列表),并向其添加一个新值并保存它。您应该删除
p.childrenKeys
赋值:
class Parent(db.Model):
childrenKeys = db.ListProperty(str,indexed=False,default=[])
p = Parent.get_or_insert('somekey')
p.childrenKeys.append('newchkey')
p.put()
替换此项:
p.childrenKeys = p.childrenKeys.append('newchildkey')
为此:
p.childrenKeys.append('newchildkey')
append()?Python中的空列表不可能等同于无,是吗?p.childrenKeys确实返回空列表。p、 append()返回None,因为这是列表的append()方法的行为。这里的问题是将None分配给p.childrenKeys,这是ListProperty不允许的。(您可以将空列表分配给ListProperty,数据存储中的ListProperty表示为没有该名称的属性。)在任何情况下,您可能需要的是StringListProperty
,而不是ListProperty(str)
。(尽管如果这对您来说是可行的,但在最近的SDK中可能发生了一些变化,使它们变得等效)。