In google app engine’s cloud based Datastore db.ReferenceProperty can be used in a Model to reference another Model like this:
from google.appengine.ext import db
b = db.ReferenceProperty(B)
name = db.StringProperty()
In the above example A references B, if you have an object of type A you can directly access its referenced B like this:
b_name = a.b.name
When you access a.b, GAE will automatically fetch b from the datastore without you having to do anything special. This is handy, but sometimes you may not want the automatic datastore fetch to happen, instead you may just want know the referenced object’s key.
If you were dealing with 1000s of objects, the extra DB fetch for each object could add a huge unnecessary time overhead to your processing (for example if you were using memcache for caching, or if you already had a list of all the relevant b’s in memory).
So how do we retrieve the key of the referenced object without triggering an datastore fetch? To achieve we have to invoke a rather obtuse function called (wait for it): get_value_for_datastore()
For example, We can find out what b’s key value is like this:
b_key = A.b.get_value_for_datastore(a)
The above won’t cause GAE to fetch the referenced B from the datastore. Its all a bit long winded and confusing but it does seem to work!