Functions which can be used inside a query

Here is the list of functions that can be used inside a generator query:

Examples:

  1. select(avg(c.orders.total_price) for c in Customer)
  1. SELECT AVG("order-1"."total_price")
  2. FROM "Customer" "c"
  3. LEFT JOIN "Order" "order-1"
  4. ON "c"."id" = "order-1"."customer"
  1. select(o for o in Order if o.customer in
  2. select(c for c in Customer if c.name.startswith('A')))[:]
  1. SELECT "o"."id", "o"."state", "o"."date_created", "o"."date_shipped",
  2. "o"."date_delivered", "o"."total_price", "o"."customer"
  3. FROM "Order" "o"
  4. WHERE "o"."customer" IN (
  5. SELECT "c"."id"
  6. FROM "Customer" "c"
  7. WHERE "c"."name" LIKE 'A%'
  8. )

Using getattr()

getattr() is a built-in Python function, that can be used for getting the attribute value.

Example:

  1. attr_name = 'name'
  2. param_value = 'John'
  3. select(c for c in Customer if getattr(c, attr_name) == param_value)