Accessing related object key without fetching object in App Engine

后端 未结 2 517
自闭症患者
自闭症患者 2021-01-05 06:21

In general, it\'s better to do a single query vs. many queries for a given object. Let\'s say I have a bunch of \'son\' objects each with a \'father\'. I get all the \'son\'

2条回答
  •  时光说笑
    2021-01-05 06:39

    I'd rather loop through the sons and get parent's keys using son.parent_key().

    parent_key()

    Returns the Key of the parent entity of this instance, or None if this instance does not have a parent.

    Since all the path is saved in the instance's key, theoretically, there is no need to hit the database again to get the parent's key.

    After that, it's possible to get all parents' instances at once using db.get().

    get(keys)

    Gets the entity or entities for the given key or keys, of any Model.

    Arguments:

    keys A Key object or a list of Key objects.

    If one Key is provided, the return value is an instance of the appropriate Model class, or None if no entity exists with the given Key. If a list of Keys is provided, the return value is a corresponding list of model instances, with None values when no entity exists for a corresponding Key.

提交回复
热议问题