Stay organized with collections Save and categorize content based on your preferences.
This page describes how to use the legacy bundled services and APIs. This API can only run in first-generation runtimes in the App Engine standard environment. If you are updating to the App Engine Python 3 runtime, refer to the migration guide to learn about your migration options for legacy bundled services.A Query
object represents an NDB query, a request for a filtered, sorted list of entities.
This page contains reference documentation. For a general discussion of NDB queries, see Queries.
Query OptionsMany query methods take a standard set of additional options, either in the form of keyword arguments such as keys_only=True
, or as QueryOptions
object passed with options=QueryOptions(...)
.
Queries support a variety of configuration options. These are specified by keyword arguments to the Query
methods:
bool
False
All operations return keys instead of entities. projection tuple (or list) of properties (or strings) None
Operations return entities with only the specified properties set. projection=[Article.title, Article.date]
or projection=['title', 'date']
fetches entities with just those two fields set. (See Projection Queries.) offset int
0
Number of query results to skip. limit int
No limit Maximum number of query results to return. batch_size int
20
Batch size.
Affects efficiency of queries only; larger batch sizes use more memory but make fewer RPC calls.
prefetch_sizeint
None
Overrides batch size for first batch returned. produce_cursors bool
False
Generate cursors from query (see Query Iterators. Query Cursors). start_cursor Cursor
None
Starting point for search (see Query Cursors). end_cursor Cursor
None
Ending point for search (see Query Cursors). deadline int
Depends on Context
Overrides RPC deadline (which defaults to 5 seconds if not overridden when Context
created) read_policy ndb.EVENTUAL_CONSISTENCY
The read policy. Set to ndb.EVENTUAL_CONSISTENCY
to get perhaps-quicker results without waiting for the Datastore to apply pending changes to all returned records.
To run a query with a specific set of options, pass the keyword arguments to the applicable method:
qry = Employee.query().filter(...).order(...) # Create a query for acct in qry.fetch(10, offset=20): # Skip the first 20 print acct
Occasionally, you might want to keep a set of query options around and use them in various places. While you could just keep them in a dictionary and pass this dictionary to the methods using **kwds
, you can also create a QueryOptions
object and pass it using the options keyword argument. The following two examples are equivalent:
qo = ndb.QueryOptions(keys_only=True, offset=20) results = qry.fetch(10, options=qo) results = qry.fetch(10, keys_only=True, offset=20)Constructor
Typically, an application creates a Query
by calling Model.query()
. But it's also possible to call ndb.Query()
.
Arguments
datastore_query.Order
object.
QueryOptions
object giving default query options to be used when the query is executed.
Query
with additional filter(s) applied. Takes filter arguments as described in Queries. qry.filter(filter1).filter(filter2)
is equivalent to qry.filter(filter1, filter2)
None
). This is similar to calling q.fetch(1)
and returning the first item of the list of results.
Arguments
Query
with additional sort order(s) applied. Takes one or more arguments which are properties or "negated" properties. For example, to sort users by age and "break ties" by name, you might use something like qry.order(-Account.birthday, Account.name)
:1
, :2
, etc.) or named bindings (:foo
, :bar
, etc.). It binds the passed values to the parameters.
To bind parameters, you might call something like qry.bind("USA", 49)
. To bind named parameters, you might call something like qry.bind(region = "USA", threshold = 49)
.
Returns a new Query
object with the parameter values bound.
len(q.fetch(limit))
but more efficiently.
Arguments
Future
whose result is a number. This is the asynchronous version of count().
Arguments
Future
whose result is a list of results. This is the asynchronous version of fetch().
Arguments
(results, cursor, more)
:
None
.bool
indicating whether there are (likely) more results after this batch. If False
, there are no more results; if True
, there are probably more results.To fetch the next page, pass the cursor returned by one call to the next call using start_cursor=cursor
. A common idiom is to pass the cursor to the client using cursor.urlsafe()
and to reconstruct that cursor on a subsequent request using Cursor(urlsafe=string)
.
None
). This is the asynchronous version of get().
Arguments
Returns a QueryIterator object.
Arguments
True
, calls the callback with batch information arguments as described below.
Future
subclass; see below.
Callback signature The callback is normally called with an entity as argument. However, if keys_only=True
is given, it is called with a Key. If pass_batch_into_callback=True
is given, the callback is called with three arguments: the current batch, the index within the batch, and the entity or Key
at that index. The callback can return whatever it wants. If the callback is None
, a trivial callback is assumed that just returns the entity or key passed in.
Optional merge_future
The merge_future
is an advanced argument that can be used to override how the callback results are combined into the overall map()
return value. By default, a list of callback return values is produced. By substituting one of a small number of specialized alternatives you can arrange otherwise. See the source code for tasklets.MultiFuture
for the default implementation and a description of the protocol the merge_future
object must implement. Alternatives from the same module include QueueFuture
, SerialQueueFuture
and ReducingFuture
.
Returns a list of the results of all the callbacks. (But see 'optional merge_future
' above.) It returns when the query has run to completion and all callbacks have returned.
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-08-07 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-08-07 UTC."],[[["This documentation covers how to use NDB queries within the legacy App Engine standard environment's first-generation runtimes, noting that for App Engine Python 3, a migration guide is recommended."],["An NDB `Query` object is used to request a filtered and sorted list of entities, and you can create one using the `\u003cvar\u003eModel\u003c/var\u003e.query()` method or directly using `ndb.Query()`."],["Queries can be configured using various keyword arguments or a `QueryOptions` object, allowing for customizations such as returning only keys (`keys_only`), limiting results (`limit`), skipping results (`offset`), or specifying a data projection (`projection`)."],["The `Query` object has several instance methods like `filter()`, `order()`, `get()`, `count()`, `fetch()`, `fetch_page()`, `map()` and their async versions, allowing for advanced query customization, execution, and data retrieval."],["Query methods often accept keyword arguments, such as `limit`, `offset`, `keys_only`, and `projection`, to refine the results, and these same options can be used in a `QueryOptions` object."]]],[]]
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4