Accessing related object key without fetching object in App Engine

后端 未结 2 538
自闭症患者
自闭症患者 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:31

    You can find the answer by studying the sources of appengine.ext.db in your download of the App Engine SDK sources -- and the answer is, no, there's no special-casing as you require: the __get__ method (line 2887 in the sources for the 1.3.0 SDK) of the ReferenceProperty descriptor gets invoked before knowing if .key() or anything else will later be invoked on the result, so it just doesn't get a chance to do the optimization you'd like.

    However, see line 2929: method get_value_for_datastore does do exactly what you want!

    Specifically, instead of son.father.key(), use Son.father.get_value_for_datastore(son) and you should be much happier as a result;-).

提交回复
热议问题