App Engine 查询使用索引引用检索数据

发布于 2024-11-04 11:31:49 字数 205 浏览 4 评论 0原文


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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

城歌 2024-11-11 11:31:49

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.

梦途 2024-11-11 11:31:49

您可以使用 fetch() Query 实例上的方法:

class Entry(Base):
    amount = db.IntegerProperty()


entries = Entry.gql("WHERE amount > 0").fetch(5)

print entries[4].amount

You could use the fetch() method on the Query instance:

class Entry(Base):
    amount = db.IntegerProperty()


entries = Entry.gql("WHERE amount > 0").fetch(5)

print entries[4].amount
泛泛之交 2024-11-11 11:31:49

你必须执行 fetch() 。这将为您提供一个条目列表。在这种情况下, my_entry=entries[4] 将为您提供第五个对象。您想要做的是操纵 gql 对象。这显然行不通。试试这个

class Entry(Base):
    amount = db.IntegerProperty()


entries = Entry.gql("WHERE amount > 0").fetch(1000)

print entries[4].amount

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

class Entry(Base):
    amount = db.IntegerProperty()


entries = Entry.gql("WHERE amount > 0").fetch(1000)

print entries[4].amount
三生殊途 2024-11-11 11:31:49

如果您想在结果查询中引用特定索引的一个对象,可以使用 db.Queryfetch 方法以及 offset 参数:

entry = Entry.gql("WHERE amount > 0").fetch(1, offset=4)[0]
print entry.amount

但是,如果您想从查询结果中引用多个对象,请获取它们全部并作为普通Python数组进行索引:

entries = Entry.gql("WHERE amount > 0").fetch(1000)
print entries[4].amount
print entries[5].amount
print entries[7].amount
# etc.

If you want to refer to one object of specific index in your result query, you can use the fetch method of db.Query with offset parameter:

entry = Entry.gql("WHERE amount > 0").fetch(1, offset=4)[0]
print entry.amount

However, if you want to refer to the multiple objects from the query results, fetch them all and index as normal Python array:

entries = Entry.gql("WHERE amount > 0").fetch(1000)
print entries[4].amount
print entries[5].amount
print entries[7].amount
# etc.
稍尽春風 2024-11-11 11:31:49
entries= [entry for entry from Entry.all() if entry.amount > 0]
print entries[4]
entries= [entry for entry from Entry.all() if entry.amount > 0]
print entries[4]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文