App Engine 查询使用索引引用检索数据
class Entry(Base):
amount = db.IntegerProperty()
entries = Entry.gql("WHERE amount > 0")
有没有一种方法可以将索引作为数组引用条目结果,例如
my_entry = entries[4]
class Entry(Base):
amount = db.IntegerProperty()
entries = Entry.gql("WHERE amount > 0")
Is there a way to refer to entries result with an index as an array, for example
my_entry = entries[4]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
entries = [x for x in Entry.gql("WHERE amount > 0")]
这个答案与之前的答案之间的区别在于,它在数据存储中而不是在处理程序中进行过滤,并且不会不需要您猜测将返回的实体的最大数量。
entries = [x for x in Entry.gql("WHERE amount > 0")]
The distinction between this and previous answers is that it filters at the datastore rather than in the handler, and doesn't require you to guess the maximum number of entities that will be returned.
您可以使用 fetch()
Query
实例上的方法:You could use the fetch() method on the
Query
instance:你必须执行 fetch() 。这将为您提供一个条目列表。在这种情况下, my_entry=entries[4] 将为您提供第五个对象。您想要做的是操纵 gql 对象。这显然行不通。试试这个
You have to do a fetch() . which will give you a list of entries . In that case my_entry=entries[4] will give you the fifth object. What you were trying to do is manipulating the gql object. Which obviously won't work. Try this
如果您想在结果查询中引用特定索引的一个对象,可以使用
db.Query
的fetch
方法以及offset
参数:但是,如果您想从查询结果中引用多个对象,请
获取
它们全部并作为普通Python数组进行索引:If you want to refer to one object of specific index in your result query, you can use the
fetch
method ofdb.Query
withoffset
parameter:However, if you want to refer to the multiple objects from the query results,
fetch
them all and index as normal Python array: