获取NDB查询长度 - 在Google App Engine上使用Python [英] Get NDB query length - using Python on Google App Engine

查看:94
本文介绍了获取NDB查询长度 - 在Google App Engine上使用Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在谷歌应用引擎上使用NDB时,获取查询结果数量的好方法是什么?

尝试这样做:如果len(query)> b

Attempted this:

query = NDB_Model.query(NDB_Model.some_property == some_value)
if len(query) > 0:    # <-- this throws and exception
    entity = query[0]

我很抱歉,这可能是一个非常简单的问题,但我不清楚从 docs

I apologize that this is probably a very simple question, but it was not clear to me from the docs.

推荐答案

好像你只是想从你的查询中获得第一个实体。这就是 query.get() $ b

It seems like you just want to get the first entity from your query. That's what query.get() is for.

query = NDB_Model.query(NDB_Model.some_property == some_value)

entity = query.get()
if entity is not None:
    # Do stuff

从文档:

From the docs:


返回第一个查询结果,否则返回None。这类似于调用q.fetch(1)并返回结果列表的第一项。

Returns the first query result, if any (otherwise None). This is similar to calling q.fetch(1) and returning the first item of the list of results.

,有 query.fetch(n)其中 n 是要获取的实体的最大数量。它返回一个列表,所以你可以很容易地检查 len()

In a more general form, there's query.fetch(n) where n is the maximum number of entities to fetch. It returns a list, so you could easily check len() on that.

这篇关于获取NDB查询长度 - 在Google App Engine上使用Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆