Problem statement
In this Django project, implement the category report's two tables as grouped queries. Build category_rows() and monthly_sales_rows() in app/queries.py so each table arrives in one query with the database doing the counting and averaging. This replaces a report that fires one query per category and grows every time a category is added, and it adds the sales-by-month table that was never written.
Requirements
Implement:
- category_rows() in app/queries.py
- monthly_sales_rows() in app/queries.py
The following files are already implemented and provided: app/models.py, app/views.py, app/urls.py and the template. Category holds a name; Product holds a name, a price and a category (reverse name products); Sale holds an amount, a sold_on date and a product. Sale also declares ordering = ("-sold_on",) in its Meta, which a grouped query drops rather than honouring. category_report calls both functions and hands their rows to the template, which reads product_count and average_price off every category row and month, sale_count and total_amount off every monthly row. Both functions return their rows; nothing prints and nothing is cached.
1. category_rows() - the annotated categories
- Returns every category with two annotations, named exactly product_count and average_price. The keyword is the attribute, so renaming it leaves the template reading nothing.
- product_count is how many products the category holds; average_price is the average of their prices.
- A category with no products is still a row, with product_count of 0 and average_price of None. Do not coalesce the average to 0, and do not filter on the relation: filter(products__isnull=False) throws that category away.
- Most products first. Two categories holding the same number of products are ordered by name ascending, because ties without a second key come back in whatever order the database felt like.
- The whole table is one query. That includes reading both annotations off every row: a .count() or a price lookup per row is one more query each, even though the numbers are right.
- The SELECT list of that statement - the part before the FROM - has to contain COUNT( and AVG(, and the statement has to carry a GROUP BY, which is what collapses the joined product rows back into one row per category. One join down to the products is also one query, but then Python does the counting, which is what is being replaced.
- Average prices are compared rounded to two decimal places, so trailing zeros and the exponent the backend chooses never matter.
2. monthly_sales_rows() - the grouped months
- Returns one row per month that has sales, oldest month first. A month without sales is absent, because there is no row to group.
- Each row is a values() dictionary with exactly the keys month, sale_count and total_amount.
- month is the first day of that month, sale_count is how many sales fall in it, total_amount is the sum of their amount. Sales made on different days of one month belong to one row.
- This table is one grouped query too. Its SELECT list has to contain COUNT( and SUM(; its GROUP BY has to name exactly one thing, because two means a second column joined the grouping and shattered the months; and it has to ORDER BY that same one thing, ascending.
Notes
- The ordering of a grouping has to be in the SQL itself: sqlite hands back the groups of a one-column GROUP BY in ascending key order whether you asked for it or not, so a grouping with no ORDER BY only looks correct until the backend changes.
- A field the query orders by joins the GROUP BY: ordering a monthly grouping by month and then by sold_on groups by both and shatters the result into one row per day. Order a grouping by the columns it groups on, or by its own aggregates, and by nothing else.
- Meta.ordering is a special case: since Django 3.1 it is dropped from a grouped query instead of joining its GROUP BY, so it cannot shatter the rows - but it does not order them either, which is why the monthly order has to be stated.
- Build the numbers in the database with annotate(), not in Python.
