Add, Update, Delete from a ndb.KeyProperty() - Google Cloud Datastore NDB

我的未来我决定 提交于 2019-12-24 07:09:26

问题


Here are my many-to-many relationship models:

class ModelA(ndb.Model):
    name = ndb.StringProperty(required=true)
    model_b = ndb.KeyProperty(kind=ModelB,repeated=True)


class ModelB(ndb.Model):
    name = ndb.StringProperty(required=true)
    model_a = ndb.KeyProperty(kind=ModelA,repeated=True)

My question is, how do I add/update/delete a single (or many) KeyProperty from let's say model_b?


回答1:


I managed to do it like this:

pos = ModelA.model_b.index(ndb.Key('ModelB',213)) # Get position from list
ModelA.model_b.pop(pos) # Remove from list
ModelA.put() # Update


来源:https://stackoverflow.com/questions/22819992/add-update-delete-from-a-ndb-keyproperty-google-cloud-datastore-ndb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!