python 的追加是否与 ndb.StringProperty(repeated=True) 一起使用?
does python's append work with ndb.StringProperty(repeated=True)?
快速提问:
我知道 google 数据存储中的 ndb.StringProperty(repeated=True) 在 python 中被视为列表。但我想知道的是,假设你有这个 class:
class Customer(ndb.Model):
name = StringProperty()
items = StringProperty(repeated=True)
并且您创建了一个实例:
custmr = Customer()
custmr.name = "Sam"
custmr.items = ['python','java','ruby']
custmr.put()
并保存...稍后检索...
q = Customer.query.filter(name="Sam")
custmr = q.get()
我可以这样做吗...
custmr.items.append('perl')
custmr.put()
并更新列表??
如果没有,那我该怎么做呢?
The docs 说你可以就地改变列表:
When updating a repeated property, you can either assign it a new list or mutate the existing list in place. When you assign a new list, the types of the list items are validated immediately. Invalid item types (for example, assigning [1, 2]
to art.tags
above) raise an exception. When you mutate the list, the change is not validated immediately. Instead, the value will be validated when you write the entity to the Datastore.
快速提问: 我知道 google 数据存储中的 ndb.StringProperty(repeated=True) 在 python 中被视为列表。但我想知道的是,假设你有这个 class:
class Customer(ndb.Model):
name = StringProperty()
items = StringProperty(repeated=True)
并且您创建了一个实例:
custmr = Customer()
custmr.name = "Sam"
custmr.items = ['python','java','ruby']
custmr.put()
并保存...稍后检索...
q = Customer.query.filter(name="Sam")
custmr = q.get()
我可以这样做吗...
custmr.items.append('perl')
custmr.put()
并更新列表??
如果没有,那我该怎么做呢?
The docs 说你可以就地改变列表:
When updating a repeated property, you can either assign it a new list or mutate the existing list in place. When you assign a new list, the types of the list items are validated immediately. Invalid item types (for example, assigning
[1, 2]
toart.tags
above) raise an exception. When you mutate the list, the change is not validated immediately. Instead, the value will be validated when you write the entity to the Datastore.