Python Advanced Data Operations
The examples combine generated School Management APIs into application-sized async workflows. Use model-aware Assist for names from a different model.
Search page with rows and two facets
rows = await (
Q.schools()
.with_name_containing("Primary")
.select_school_type_with(
Q.school_types_minimal().select_code().select_name()
)
.select_platform_with(Q.platforms_minimal().select_name())
.facet_by_school_type_as(
"schoolTypes",
Q.school_types_minimal()
.with_code_in("PRIMARY", "SECONDARY")
.select_code()
.select_name()
.count_schools_as("schoolCount"),
include_all_facets=True,
)
.facet_by_platform_as(
"platforms",
Q.platforms_minimal().select_name().count_schools_as("schoolCount"),
include_all_facets=False,
)
.order_by_id_descending()
.limit(50)
.comment("Search schools with dashboard facets")
.purpose("Render the authorized operations dashboard")
.execute_for_list(context)
)
type_buckets = rows.facet("schoolTypes")
platform_buckets = rows.facet("platforms")
The SchoolType Facet preserves allowed zero-count buckets; the Platform Facet returns matched-only buckets. Neither replaces the primary School list.
Deep graph and per-parent Top-N
platforms = await (
Q.platforms()
.select_name()
.select_school_list_with(
Q.schools()
.select_name()
.select_school_type_with(
Q.school_types_minimal().select_code().select_name()
)
.order_by_student_capacity_descending()
.limit(3)
)
.comment("Load each platform and its three largest schools")
.purpose("Render the authorized platform capacity review")
.execute_for_list(context)
)
The nested .limit(3) means three Schools per Platform. The SQL provider uses
its partition/window plan rather than loading every child and slicing in Python.
Grouped and relation analytics
distribution = await (
Q.schools()
.group_by_school_type()
.count_as("schoolCount")
.sum_student_capacity_as("capacityTotal")
.avg_student_capacity_as("capacityAverage")
.comment("Aggregate school capacity by type")
.purpose("Build the authorized capacity report")
.execute_for_rows(context)
)
cards = await (
Q.school_types_minimal()
.select_code()
.select_name()
.count_schools_as("schoolCount")
.sum_student_capacity_of_schools_as("capacityTotal", Q.schools())
.comment("Calculate type-level school metrics")
.purpose("Render the authorized type cards")
.execute_for_list(context)
)
distribution contains dictionaries keyed by group fields and aggregate
aliases. cards contains typed SchoolTypes decorated with relation metrics.
Audited read-modify-save
school = await (
Q.schools()
.with_id_is(school_id)
.select_platform_with(Q.platforms_minimal().select_name())
.select_school_type_with(Q.school_types_minimal().select_code())
.comment("Load the complete school for capacity approval")
.purpose("Apply an authorized capacity revision")
.execute_for_one(context)
)
if school is None:
raise LookupError("school not found")
school.update_student_capacity(new_capacity)
school = await school.audit_as(
"Approve revised student capacity"
).save(context)
Load all Checker/Fix inputs and keep the saved entity so the next mutation uses the authoritative optimistic version.