How to get to the key name of a referenced entity property from an entity instance without a datastore read in google app engine?
- by Sumeet Pareek
Consider I have the following models -
class Team(db.Model): # say I have just 5 teams
name = db.StringProperty()
class Player(db.Model): # say I have thousands of players
name = db.StringProperty()
team = db.ReferenceProperty(Team, collection_name="player_set")
Key name for each Team entity = 'team_' , and for each Player entity = 'player_'
By some prior arrangement I have a Team entity's (key_name, name) mapping available to me. For example (team_01, United States Of America), (team_02, Russia) etc
I have to show all the players and their teams on a page. One way of doing this would be -
players = Player.all().fetch(1000) # This is 1 DB read
for player in players: # This will iterate 1000 times
self.response.out.write(player.name) # This is obviously not a DB read
self.response.out.write(player.team.name) #This is a total of 1x1000 = 1000 DB reads
That is a 1001 DB reads for a silly thing.
The interesting part is that when I do a db.to_dict() on players, it shows that for every player in that list there is 'name' of the player and there is the 'key_name' of the team available too.
So how can I do the below ??
players = Player.all().fetch(1000) # This is 1 DB read
for player in players: # This will iterate 1000 times
self.response.out.write(player.name) # This is obviously not a DB read
self.response.out.write(team_list[player.<SOME WAY OF GETTING TEAM KEY NAME>]) # Here 'team_list' already has (key_name, name) for all 5 teams
I have been struggling with this for a long time. Have read every available documentation.
I could just hug the person that can help me here :-)
Disclaimer: The above problem description is not a real scenario. It is a simplified arrangement that represents my problem exactly. I have run into it in a rater complex and big GAE appication.