简体   繁体   English

Google App Engine NDB连续查询

[英]google app engine ndb successive queries

Suppose I have an HTML form as follows: 假设我有一个HTML表单,如下所示:

<form ...>
    Title: <input type=text name=title>
    Account: <input type=text name=account>
    <input type=submit value=search>
</form>

The NDB of the google app engine is as follows: google app引擎的NDB如下:

class Message(ndb.Model):
    title = ndb.StringProperty()
    account = ndb.StringProperty()

Since the user may or may not input data in the fields, the search would be implemented as successive queries: 由于用户可能会或可能不会在字段中输入数据,因此搜索将实现为连续查询:

messages = Message.query()
if title:
    messages = messages.query(Message.title==title)
if account:
    messages = messages.query(Message.account==account)

The above code didn't work because "Query object has no attribute query" 上面的代码不起作用,因为“查询对象没有属性查询”

So, how do I perform successive queries? 那么,如何执行连续查询?

Have you read the docs for ndb queries. 您是否阅读过有关ndb查询的文档。 https://developers.google.com/appengine/docs/python/ndb/queries https://developers.google.com/appengine/docs/python/ndb/queries

It has a good example of constructing a query in multiple parts 它有一个很好的例子,可以将查询分为多个部分

qry1 = Account.query() # Retrieve all Account entitites
qry2 = qry1.filter(Account.userid >= 40) # Filter on userid >= 40
qry3 = qry2.filter(Account.userid < 50) # Filter on userid < 50 as well

In your example you would 在您的示例中,您将

messages = Message.query()
if title:
    messages = messages.filter(Message.title==title)
if account:
    messages = messages.filter(Message.account==account)

Calling query() in the code you have in the question creates a new query, rather than extending it. 在问题代码中调用query()会创建一个新查询,而不是扩展它。

Read further in the docs, and you can see you can also start using OR operators to construct the query in multiple phases. 在文档中进一步阅读,您会发现您也可以开始使用OR运算符来分多个阶段构造查询。 The example above means AND. 上面的示例表示AND。

摆脱messages = Message.query()行,然后使用:

messages = Message.query(Message.title==title)

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

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