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

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

问题描述

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

尝试过:

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

很抱歉,这可能是一个非常简单的问题,但我从 文档.

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

推荐答案

您似乎只想从查询中获取第一个实体.这就是 query.get() 是为了.

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

来自文档:

返回第一个查询结果,如果有的话(否则为 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天全站免登陆