Skip to content

Latest commit

 

History

History
217 lines (132 loc) · 7.2 KB

querying.rst

File metadata and controls

217 lines (132 loc) · 7.2 KB

Querying

Get one entity identified by its key value

Get employee identified by 1 and print employee first name:

employee1 = northwind.entity_sets.Employees.get_entity(1).execute()
print(employee1.FirstName)

Get one entity identified by its key value which is not scalar

Get number of orderd units in the order identified by ProductID 42 and OrderID 10248:

order = northwind.entity_sets.Order_Details.get_entity(OrderID=10248, ProductID=42).execute()
print(order.Quantity)

Get all entities of an entity set

Print unique identification (Id) and last name of all employees:

employees = northwind.entity_sets.Employees.get_entities().select('EmployeeID,LastName').execute()
for employee in employees:
    print(employee.EmployeeID, employee.LastName)

Get entities matching a filter

Print unique identification (Id) of all employees with name John Smith:

smith_employees_request = northwind.entity_sets.Employees.get_entities()
smith_employees_request = smith_employees_request.filter("FirstName eq 'John' and LastName eq 'Smith'")

for smith in smith_employees_request.execute():
    print(smith.EmployeeID)

Get entities matching a filter in more Pythonic way

Print unique identification (Id) of all employees with name John Smith:

from pyodata.v2.service import GetEntitySetFilter as esf

smith_employees_request = northwind.entity_sets.Employees.get_entities()
smith_employees_request = smith_employees_request.filter(esf.and_(
                                                         smith_employees_request.FirstName == 'Jonh',
                                                         smith_employees_request.LastName == 'Smith'))
for smith in smith_employees_request.execute():
    print(smith.EmployeeID)

Get entities matching a filter in ORM style

Print unique identification (Id) of all employees with name John Smith:

from pyodata.v2.service import GetEntitySetFilter as esf

smith_employees_request = northwind.entity_sets.Employees.get_entities()
smith_employees_request = smith_employees_request.filter(FirstName="John", LastName="Smith")
for smith in smith_employees_request.execute():
    print(smith.EmployeeID)

Get entities matching a complex filter in ORM style

Print unique identification (Id) of all employees with name John Smith:

from pyodata.v2.service import GetEntitySetFilter as esf

smith_employees_request = northwind.entity_sets.Employees.get_entities()
smith_employees_request = smith_employees_request.filter(FirstName__contains="oh", LastName__startswith="Smi")
for smith in smith_employees_request.execute():
    print(smith.EmployeeID)

Grouping ORM filter conditions

Pass group=True to keep each condition grouped when combining and and or. This is useful for __in lookups, which expand to alternatives:

request = service.entity_sets.Employees.get_entities().filter(
    ID__in=[2, 3], NameLast="Smith", group=True)
# $filter: ((ID eq 2 or ID eq 3) and (NameLast eq 'Smith'))

Both alternatives must now also match NameLast. Without grouping, OData's operator precedence lets the first alternative match regardless of the name. The same option groups Q arguments and conditions inside a Q object:

from pyodata.v2.service import FilterExpression as Q

request = service.entity_sets.Employees.get_entities().filter(
    Q(ID=2) | Q(ID=3), NameLast="Smith", group=True)

On a chained call, group=True also groups the whole preceding filter before adding the new conditions. A string filter still replaces the preceding filter, as it does without grouping. Grouping is selected per call and defaults to False, preserving existing query serialization.

group is a filter option; to filter an entity property with that exact name, pass it inside Q(group=value).

Get a count of entities

Print a count of all employees:

count = northwind.entity_sets.Employees.get_entities().count().execute()
print(count)

Print all employees and their count:

employees = northwind.entity_sets.Employees.get_entities().count(inline=True).execute()
print(employees.total_count)

for employee in employees:
    print(employee.EmployeeID, employee.LastName)

Get a count of entities via navigation property

Print a count of all orders associated with Employee 1:

count = northwind.entity_sets.Employees.get_entity(1).nav('Orders').get_entities().count().execute()
print(count)

Print all orders associated with Employee 1 and their count:

orders = northwind.entity_sets.Employees.get_entity(1).nav('Orders').get_entities().count(inline=True).execute()
print(orders.total_count)

for order in orders:
    print(order.OrderID, order.ProductID)

Use non-standard OData URL Query parameters

Sometimes services implement extension to OData model and require addition URL query parameters. In such a case, you can enrich HTTP request made by pyodata with these parameters by the method custom(name: str, value: str).

employee = northwind.entity_sets.Employees.get_entity(1).custom('sap-client', '100').execute()
employees = northwind.entity_sets.Employees.get_entities().custom('sap-client', '100').custom('$skiptoken', 'ABCD').top(10).execute()

Encode OData URL Path

By default the resource paths of requests are percent encoded. However if this is not what your API expects, you can disable the encoding with the variable encode_path by setting it to False.

employee = northwind.entity_sets.Employees.get_entity(1, encode_path=False).execute()

Query server-side paginations using the __next field

Response may contains ony partial listings of the Collection. In this case, "__next" name/value pair is included, where the value is a URI which identifies the next partial set of entities.

employees = northwind.entity_sets.Employees.get_entities().select('EmployeeID,LastName').execute()
while True:
    for employee in employees:
     print(employee.EmployeeID, employee.LastName)

    # Stop if server has no more entities left
    if employees.next_url is None:
      break

    # We got a partial answer - continue with next page
    employees = northwind.entity_sets.Employees.get_entities().next_url(employees.next_url).execute()