简体   繁体   English

Google Appengine数据存储树结构

[英]Google appengine datastore tree structure

I need to be able to make a tree like structure in the appengine database. 我需要能够在appengine数据库中制作树状结构。
I have try to make an object reference itself but have not gotten it to work. 我试图做一个对象引用本身,但还没有使它起作用。

class Item(db.Model):
    children = db.ListProperty(db.ReferenceProperty(Item))

Alternatively, you can store references to children in the parent with: 另外,您可以使用以下方式将对孩子的引用存储在父对象中:

class Node(db.Model):
    children = db.ListProperty(db.Key)

This answer shamelessly stolen (with credit!) from Nick Johnson's answer to this related question 尼克·约翰逊(Nick Johnson)对这个相关问题的回答,使这个回答无耻地被偷了(功劳!)

Here is a related topic from the google-appengine group. 是google-appengine组的相关主题。

You can store a reference to the parent node in each child, instead of references to the child nodes in the parent. 您可以在每个子节点中存储对父节点的引用,而不是在父节点中存储对子节点的引用。

Here's some code: 这是一些代码:

class Node(db.Model):
    pass

...snip...

root = Node()
db.put(root)

for i in xrange(10):
    child = Node(parent=root)
    db.put(child)

    for i in xrange(5):
        grandchild = Node(parent=child)
        db.put(grandchild)

parent is a special field on a Model which tells the datastore that an entity has a parent-child relationship with its parent. parentModel上的一个特殊字段,它告诉数据存储区一个实体与其父级具有父子关系。

From the docs : 文档

When the application creates an entity, it can assign another entity as the parent of the new entity, using the parent argument in the Model constructor. 应用程序创建实体时,可以使用Model构造函数中的parent参数将另一个实体分配为新实体的父代。 Assigning a parent to a new entity puts the new entity in the same entity group as the parent entity. 将父实体分配给新实体会将新实体与父实体置于同一实体组中。

An entity without a parent is a root entity. 没有父级的实体是根实体。 An entity that is a parent for another entity can also have a parent. 作为另一个实体的父实体的实体也可以具有父实体。 A chain of parent entities from an entity up to the root is the path for the entity, and members of the path are the entity's ancestors. 从实体到根的父实体链是该实体的路径,而路径的成员是该实体的祖先。 The parent of an entity is defined when the entity is created, and cannot be changed later. 实体的父代是在创建实体时定义的,以后无法更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM