简体   繁体   English

Google App Engine(python):确定查询是否返回结果的最佳方法

[英]Google App Engine (python): Best way to determine if a query returns results or not

I'm using the webapp framework on GAE, and to show the results of a query, I'm doing a get() on the query object, then iterating on it if get() returned anything, for example: 我在GAE上使用了webapp框架,并且为了显示查询的结果,我对查询对象进行了get(),然后在get()返回任何内容的情况下对其进行迭代,例如:

query = Employee.all().filter("some_boolean_property = ", True)
if query.get():
    for employee in query:
        # output employee.name etc.
        # ...
else:
    # output "no records found" message
    # ...

The reason I'm doing the get() and not just doing an else on the for loop is because I'm outputting the data in a table, and I don't want to write the table code if there are no results. 我执行get()而不只是在for循环上执行else的原因是因为我正在将数据输出到表中,并且如果没有结果,我不想编写表代码。 Previously instead of the get() I was doing fetch(1), but I believe they are equivalent (ie, get() just executes the query but with a maximum of one result). 以前我不是使用get()来执行fetch(1),但我相信它们是等效的(即get()仅执行查询,但结果最多为一个)。 Therein lies my question - is this true that I can use get() in this way, and is this the best way to do find out if a query returns results or not? 我的问题就在这里-我是否可以通过这种方式使用get(),这是找出查询是否返回结果的最佳方法吗? Might count(1) be better? 可能数(1)会更好吗?

I'm not concerned with the number of results, just if there are any or not. 我不关心结果的数量,无论是否存在。

Just call fetch to get the number of results you need, then iterate over them. 只需调用fetch即可获取所需结果的数量,然后对其进行迭代。 For example: 例如:

query = Employee.all().filter("some_boolean_property = ", True)
results = query.fetch(20)
if results:
    for employee in results:
        # Do stuff

An alternative might be to set a flag inside the loop to say that at least one record is found, then test that flag for your "no records found" case. 一种替代方法是在循环内设置一个标志,说已找到至少一条记录,然后针对“未找到记录”的情况测试该标志。

found = False
query = Employee.all().filter("some_boolean_property = ", True)
for employee in query:
    found = True
    # output employee.name etc.
    # ...
if not found:
    # output "no records found" message
    # ...

This has the advantage of removing a call to the datastore. 这具有删除对数据存储的调用的优点。

You can use the Query class's count method. 您可以使用Query类的count方法。 If you provide a limit it will only check that many. 如果提供limit ,则只会检查那么多。 Here's an example: 这是一个例子:

query = Employee.all().filter("some_boolean_property = ", True)
if query.count(limit=1):
   for employee in query:
       pass

'count' returns just the number of results found with the query “计数”仅返回查询中找到的结果数

'get' and 'fetch' return the entire entity 'get'和'fetch'返回整个实体

so the 'count' is much more efficient if you only want the count of a query 因此,如果您只想查询的次数,则“计数”会更有效

Also I dont see why people are running a loop after a query for 1 item. 我也看不出为什么人们在查询1个项目后就运行一个循环。 If you use get() you will only receive 1 item that matches so you dont need a loop. 如果使用get(),则只会收到1个匹配的项目,因此您不需要循环。 All you are really checking is if you actual got an entity or count back from the query. 您真正要检查的只是您是否确实有实体或从查询中倒数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM