Reputation: 6594
I've got a page on my Python app which has a drop-down box of locations associated with the current user, with the names of the locations as the visible values of the drop-down box. What I want to do is allow the user to select one of these locations to associate with their current location. When a user adds a new Location it is created as a child entity of that user, so I populate the drop-down box like this:
locinfo = db.GqlQuery("SELECT * FROM Location WHERE ANCESTOR IS :1", userinfo)
...and this is how I intend to display the drop-down box on the site (the i.key value is just a placeholder, I'll explain).
<select name="lockey">
{% for i in locinfo %}
<option value="{{i.key}}">{{i.locname}}</option>
{% endfor %}
</select>
So what I want to do is add a reference to one particular Location entity in a User record. In a normal relational database I would get the primary key of each location, print them out as the value of each option, and store them in the User record as a reference to the user's current location. However I've looked around and haven't found anything about retrieving the key, and the more I look around the more I get the impression I'm going about it the wrong way.
Any suggestions?
Upvotes: 2
Views: 5970
Reputation: 36454
Keys in the datastore are represented by objects, not simple values (see here)
For any entity in the datastore, call its .key()
method to get its key object, and then you can get the printable representation by casting it to a string.
You can either set this up in your view code that gets data ready for the template, or if you want to be able to just call i.key
in your template, you can create a read-only property in that model class.
Upvotes: 2