Skip to main content
Premium

Querying the Endpoint

GET /<api_prefix>/<service_name>/admin/<resource>/aggregate

Query parameters:

ParamRequiredDescription
functionyescount, sum, avg, min, max.
fieldfor sum/avg/min/maxField to aggregate. Must include the chosen function in its aggregations whitelist. Ignored for count.
group_byno, repeatableOne or more items in the form <field>[:<bucket_fn>[:<arg>]]. Repeat to group by several keys.

group_by item forms:

  • <field> – exact value grouping (category__name).
  • <field>:trunc:<gran> – date / datetime truncation. gran is one of second, minute, hour, day, week, month, quarter, year. Bucket labels are ISO 8601 timestamps in the active Django timezone (override via TIME_ZONE or timezone.activate() middleware).
  • <field>:width:<n> – numeric fixed-width bucketing into [0, n), [n, 2n), … Bucket labels are the lower bounds.

Examples:

# Total page views
GET /admin/product/aggregate?function=sum&field=stock

# Product counts grouped by category
GET /admin/product/aggregate?function=count&group_by=category__name

# Sum of view counts per month
GET /admin/product/aggregate?function=sum&field=stock&group_by=created_at:trunc:month

# Product counts in 20-unit price buckets
GET /admin/product/aggregate?function=count&group_by=price:width:20

Response:

{
"results": [
{"group": {"category__name": "Books"}, "value": 512},
{"group": {"category__name": "Music"}, "value": 89}
]
}

For a scalar query (no group_by) group is omitted and a single bucket is returned:

{"results": [{"value": 1234}]}

The schema of valid fields and filters for a resource is exposed at /admin/<resource>/aggregate/schema. The frontend can use it to render the configuration UI of an aggregating dashboard widget.

Grouped results always carry the raw value (the stored choices value or the related row's primary key), mirroring the list endpoint. To render a human label, the aggregate schema exposes the same mapping as regular fields:

  • enum – value/label pairs for a choices field, so {"status": "draft"} can be displayed as Draft.
  • reference – the related admin resource name for a relation field, so the raw primary key in {"category": 7} can be resolved against that resource.

A hard limit on the number of returned buckets protects the server against runaway grouping queries. The default is 50 and is configurable per admin via aggregate_max_buckets. Requests producing more buckets are rejected with HTTP 422.