Querying¶
Entities can be queried from a service with a Query object:
query = Service.query(Order)
Adding filters and other options always creates a new Query object with the given directives:
>>> query.filter(Order.Name == 'Foo')
<Query for <Order>>
This makes object chaining possible:
>>> first_order = query.filter(...).filter(...).order_by(...).first()
The resulting objects can be fetched with first()
,
one()
, all()
, get()
or
just iterating the Query object itself. Network is not accessed until one of
these ways is triggered.
Navigation properties can be loaded in the same request with
expand()
:
>>> query.expand(Order.Shipper, Order.Customer)
>>> order = query.first()
API¶
-
class
odata.query.
Query
(entitycls, connection=None, options=None)¶ This class should not be instantiated directly, but from a
ODataService
object.-
all
()¶ Returns a list of all Entity instances that match the current query options. Iterates through all results with multiple requests fired if necessary, exhausting the query
Returns: A list of Entity instances
-
expand
(*values)¶ Set
$expand
query parameterParameters: values – Entity.Property
instanceReturns: Query instance
-
filter
(value)¶ Set
$filter
query parameter. Can be called multiple times. Multiplefilter()
calls are concatenated with ‘and’Parameters: value – Property comparison. For example, Entity.Property == 2
Returns: Query instance
-
first
()¶ Return the first Entity instance that matches current query
Returns: Entity instance or None
-
get
(*pk, **composite_keys)¶ Return a Entity with the given primary key
Parameters: - pk – Primary key value
- composite_keys – Primary key values for Entities with composite keys
Returns: Entity instance or None
-
limit
(value)¶ Set
$top
query parameterParameters: value – Number of records to return Returns: Query instance
-
offset
(value)¶ Set
$skip
query parameterParameters: value – Number of records to skip Returns: Query instance
-
one
()¶ Return only one resulting Entity
Returns: Entity instance
Raises: - NoResultsFound – Zero results returned
- MultipleResultsFound – Multiple results returned
-
order_by
(*values)¶ Set
$orderby
query parameterParameters: values – One of more of Property.asc() or Property.desc() Returns: Query instance
-
raw
(query_params)¶ Execute a query with custom parameters. Allows queries that
Query
does not support otherwise. Results are not converted to Entity objects>>> query = Service.query(MyEntity) >>> query.raw({'$filter': 'EntityId eq 123456'}) [{'EntityId': 123456, 'Name': 'Example entity'}]
Parameters: query_params (dict) – A dictionary of query params containing $filter, $orderby, etc. Returns: Query result
-
select
(*values)¶ Set properties to fetch instead of full Entity objects
Returns: Raw JSON values for given properties
-