I find that it's best to write in terms of keys because it's easy to
turn keys into models and fetching models when one could use a key is
a bad idea because fetching entities is so expensive.

a root_key function that takes either a model instance or a key seems
like a better idea.

Also, root models are their own ancestor (as far as queries are
concerned), so None may not be the most useful result.

def root_key_or_none(k_or_m):
    if isinstance(k_or_m, db.Key):
        parent_key = k_or_m.parent()
    else:
        parent_key = k_or_m.parent_key()
    next = parent_key
    while next:
        parent_key = next
        next = parent_key.parent()
    return parent_key

On Feb 19, 11:26 am, Rein Petersen <rein.peter...@gmail.com> wrote:
> Hey,
>
> I find myself wanting to get at the very root entity for a given Model
> that may be part of a longer entity chain. Wondering what others think
> about suggesting a feature request:
>
> add instance method to db.Model
>
> root() Returns a model instance for the root (top parent) entity
> within the entity chain of this instance, or None if this instance
> does not have a parent
>
> Thanks in advance for any comments...
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Google App Engine" group.
To post to this group, send email to google-appengine@googlegroups.com
To unsubscribe from this group, send email to 
google-appengine+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/google-appengine?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to