Sudhir Jonathan
Sudhir Jonathan

Reputation: 17516

Get only the keys out of a reference property in GAE-Py

I've got a an app where I'm storing posts and their authors. Very straightforward each post has one author model.

The problem is this: I fetch the last 10 posts using one call, using fetch() with limit = 10. But when I print them out, GAE uses 10 extra gets to access the author details, because the author object is a reference property on the post... a classic n+1 query scenario - 1 query to get 10 posts and then 10 queries to get each author.

Is there any way I can iterate over my posts to collect just the author object keys, so I can load them all up at one go using

db.get(all_author_keys)

Upvotes: 0

Views: 255

Answers (1)

Alex Martelli
Alex Martelli

Reputation: 881863

See my response to this question just a couple hours ago -- a Q almost identical by amazing coincidence by this one, though by a different poster.

In short, to do this, use the get_value_for_datastore of the Property object.

Upvotes: 1

Related Questions