谷歌应用引擎中的祖先路径ndb:子的父键与其父键不同

时间:2014-05-18 05:07:49

标签: google-app-engine app-engine-ndb

def post(self):
  #Step 1: Receive POST data
  value1 = self.request.get('value1')
  value2 = self.request.get('value2')

  #Step 2: Put the ROOT entity into the database
  root = Root(name = value1)
  root.put()

  #Step 3: Link the Child entity to it's Parent entity (the Root)...
  root_key = ndb.Key(Root, value1)
  child= Child(name = value2,parent=root_key)
  #...Then put the Child entity into the database
  child.put()

问题是来自Root(第2步)的密钥与来自Child的密钥(步骤3)不匹配。任何建议将不胜感激!

2 个答案:

答案 0 :(得分:2)

相反,请尝试使用root_key:

root_key = root.put()

答案 1 :(得分:0)

您正在使用name,您应该使用id。在创建模型时,实际上在键中设置名称,而是在属性中设置名称。因此,root_key(其中value1作为键名称与root的键不匹配,后者将自动分配ID。请参阅Model id构造函数,该构造函数具有name但没有key参数:

  

参数:

     

模型子类支持这些关键字参数:

     

id此模型的关键实例。如果使用key参数,则id和   parent必须为None(默认值)。

     

parent此模型的密钥ID。如果id是   used,key必须为None(默认值)。

     

namespace关键实例   父级模型或顶级级别的无。如果使用父级,则必须使用密钥   没有。

     

{{1}}用于此实体的命名空间,或无(默认)   使用当前命名空间。如果使用名称空间,则密钥必须为“无”。

相关问题