从应用程序引擎数据存储区检索键/ID 值?
我的 Python 应用程序上有一个页面,其中有一个与当前用户关联的位置下拉框,位置名称作为下拉框的可见值。我想要做的是允许用户选择这些位置之一来与他们当前的位置关联。当用户添加新位置时,它会创建为该用户的子实体,因此我像这样填充下拉框:
locinfo = db.GqlQuery("SELECT * FROM Location WHERE ANCESTOR IS :1", userinfo)
...这就是我打算在网站上显示下拉框的方式( i.key 值只是一个占位符,我会解释)。
<select name="lockey">
{% for i in locinfo %}
<option value="{{i.key}}">{{i.locname}}</option>
{% endfor %}
</select>
因此,我想要做的是在用户记录中添加对一个特定位置实体的引用。在普通的关系数据库中,我将获取每个位置的主键,将它们作为每个选项的值打印出来,并将它们存储在用户记录中作为对用户当前位置的引用。然而,我环顾四周,没有发现任何有关找回钥匙的信息,而且我环顾四周越多,我就越觉得我的做法是错误的。
有什么建议吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
数据存储区中的键由对象表示,而不是简单的值(请参阅此处)
对于数据存储区中的任何实体,调用其
.key()
方法来获取其键对象,然后可以通过将其转换为字符串来获取可打印的表示形式。您可以在为模板准备数据的视图代码中进行设置,或者如果您希望能够在模板中调用
i.key
,您可以创建一个只读属性在那个模型类中。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.