Query Reference¶
Query builder for the Google Cloud Datastore ODM.
This module is structured into three layers
- AST Nodes: Simple objects that represent filter/order logic.
- Logic Functions: Global AND/OR helpers to group Nodes.
- Query Engine: The main class that translates Nodes into Datastore SDK calls.
Query(model_cls, project=None, database=None, namespace=None)
¶
Bases: Generic[TModel]
The main Query builder for fetching entities from Google Cloud Datastore.
This class provides a fluent, chainable API for building complex Datastore queries using Python-native property comparisons.
projection(*args)
¶
Sets the projection fields for the query.
Projection queries are significantly faster and cheaper because they only retrieve specific fields from the Datastore rather than the entire entity.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*args
|
str | Property
|
The properties to retrieve. |
()
|
Returns:
| Name | Type | Description |
|---|---|---|
Query |
Query[TModel]
|
The chainable query instance. |
Examples:
distinct_on(*args)
¶
keys_only()
¶
Marks the query to return only Datastore Keys instead of full entities.
Keys-only queries are incredibly fast and cost-effective. Use them when you only need to check for existence or perform batch deletions.
Returns:
| Name | Type | Description |
|---|---|---|
Query |
Query[TModel]
|
The chainable query instance. |
filter(*args)
¶
Adds filters to the query.
Supports both standard ODM Property comparisons (recommended) and
raw string passthrough for edge cases.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*args
|
Node | str
|
Filter nodes generated by comparing properties, or three raw strings (name, operator, value) for passthrough. |
()
|
Returns:
| Name | Type | Description |
|---|---|---|
Query |
Query[TModel]
|
The chainable query instance. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the arguments are malformed. |
Examples:
order(*args)
¶
Adds ordering/sorting to the query.
Supports unary operators (- for descending, + for ascending) directly
on properties, or raw string field names.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*args
|
OrderNode | str | Property
|
The properties to sort by. |
()
|
Returns:
| Name | Type | Description |
|---|---|---|
Query |
Query[TModel]
|
The chainable query instance. |
Examples:
fetch(limit=None)
¶
Executes the query and yields results.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
limit
|
int | None
|
The maximum number of results to return. |
None
|
Yields:
| Type | Description |
|---|---|
TModel | Key
|
Model | datastore.Key: Hydrated model instances, or Datastore Keys |
TModel | Key
|
if |
Examples:
fetch_page(page_size, start_cursor=None)
¶
Fetches a specific page of results, returning metadata needed for pagination.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
page_size
|
int
|
The maximum number of entities to retrieve in this page. |
required |
start_cursor
|
bytes | None
|
The pagination cursor from a previous call. |
None
|
Returns:
| Type | Description |
|---|---|
tuple[list[TModel | Key], bytes | None, bool]
|
A tuple containing three elements
|
Examples:
get()
¶
Executes the query and returns the first matching result.
Automatically applies a limit=1 to the query to ensure maximum efficiency.
Returns:
| Type | Description |
|---|---|
TModel | Key | None
|
Model | datastore.Key | None: The first matching instance, or |
Examples:
count()
¶
Performs a fast server-side count aggregation.
This delegates the counting operation to Google's backend, making it infinitely more scalable and cost-effective than fetching and counting entities locally.
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
The total number of matching entities. |
sum(property_field)
¶
avg(property_field)
¶
aggregate(**kwargs)
¶
Performs multiple aggregations in a single Datastore RPC call.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**kwargs
|
CountAggregation | SumAggregation | AvgAggregation
|
Alias names mapped to Google Datastore Aggregation objects
( |
{}
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
dict[str, Any]: A dictionary mapping your aliases to their aggregated values. |
Examples:
and_(*filters)
¶
Combines multiple filters with AND logic.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*filters
|
Node
|
Two or more filter nodes. |
()
|
Returns:
| Name | Type | Description |
|---|---|---|
CompositeNode |
CompositeNode
|
A logical AND grouping of the filters. |
Examples:
or_(*filters)
¶
Combines multiple filters with OR logic.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*filters
|
Node
|
Two or more filter nodes. |
()
|
Returns:
| Name | Type | Description |
|---|---|---|
CompositeNode |
CompositeNode
|
A logical OR grouping of the filters. |
Examples: