如何在谷歌应用程序引擎中获取模型的给定实例的所有后代? [英] How to get all the descendants of a given instance of a model in google app engine?

查看:81
本文介绍了如何在谷歌应用程序引擎中获取模型的给定实例的所有后代?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在每个孩子只有一个父母的情况下,父母可以有孩子的情况下使用父子关系,并且使用 Children.all()。ancestor(parent.key)解决方案,通过在构造函数中设置parent = parent.key来构建子项?这种查询是否适用1000限制?

解决方案

查询返回您所期望的,在他们祖先的任何地方指定父母。该查询完全表达了这一点,所以我怀疑是否有更简单的方法来做同样的事情。但是,App Engine不断增加功能并令我惊讶: - )

可能需要 parent.key(),我认为这取决于你是使用Python还是使用Java。



顺便说一下,不建议使用祖先 - 父 - 子来建模数据中的关系。存在实体组以启用事务,而不是用作免费ReferenceProperty。亲子应该是一个低层次的实现细节,意思是这两个实体可能需要在单个事务中修改,或者我正在玩一个优化技巧,它允许我使用列表属性而无需加载当我得到实体时列入内存。作为一个经验法则,如果实体并不都是属于同一个用户,那么它们不应该是父子关系,因为在不同的用户试图通过不同的数据存储节点修改它们时,以这种方式引入争用会导致争用:



http://code.google.com/appengine/docs/python/datastore/keysandentitygroups.html#Entity_Groups_Ancestors_and_Path


Using parent child relationship where a parent can have children while each child has only one parent, does using Children.all().ancestor(parent.key) a good solution where a child is constructed by setting parent=parent.key in the constructor? Is the 1000 limit applies with this kind of query?

解决方案

The query returns what you'd expect, all Children which have the specified parent anywhere in their ancestry. The query expresses exactly that, so I doubt there's a simpler way of doing the same thing. But App Engine does keep adding features and surprising me :-)

Possibly you need parent.key(), I think it depends whether you're in Python or Java.

Btw, it's not recommended to use ancestor-parent-child to model relationships in your data. Entity groups exist to enable transactions, not for use as a "free" ReferenceProperty. parent-child should be a low-level implementation detail, meaning either "these two entities may need to be modified in a single transaction", or perhaps "I am playing an optimization trick which allows me to use list properties without having to load the list into memory when I get the entity". As a rule of thumb, if the entities don't all "belong" to the same user, then they shouldn't be parent-child related, because relating them in that way introduces contention when different users try modify them via different datastore nodes:

http://code.google.com/appengine/docs/python/datastore/keysandentitygroups.html#Entity_Groups_Ancestors_and_Paths

这篇关于如何在谷歌应用程序引擎中获取模型的给定实例的所有后代?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆