Problem statement
In this Django project, implement the exports page without dragging columns and rows nobody reads out of the database. Build export_rows, has_published and published_count in app/views.py so the listing stops fetching the heavy body column and the two numbers beside it are answered by the database. The page looks exactly the same afterwards.
Requirements
Implement in app/views.py:
- export_rows()
- has_published()
- published_count()
The following files are already implemented and provided: app/models.py, app/urls.py, the template, and in app/views.py the home view, published_articles() and export_page. Article holds title, slug, body and published_on, and body is the heavy column: a few kilobytes per row. published_articles() is the one filter the whole page is built on, and EXPORT_COLUMNS names the columns the page shows. /exports/ renders articles, total and has_published into the page. The three helpers keep their names, and published_articles stays the single filter they build on.
1. export_rows() - the trimmed listing
- Returns the rows the page lists, in the order published_articles() set.
- Its SQL asks for title, slug and published_on - the names in EXPORT_COLUMNS - plus the primary key, and does not ask for body.
- The rows it returns are model instances with body deferred, so get_deferred_fields() on one of them is {"body"} and the page renders without the body ever being loaded. Rows the page has already rendered are still trimmed afterwards.
- only and defer both produce the same SELECT when the only column to drop is the heavy one, so either is a correct way to get there.
2. has_published() - the flag
- Returns whether anything is ready to export.
- The query it runs stops at the first match and fetches no article columns.
- It is false when nothing is published.
3. published_count() - the total
- Returns how many articles are ready to export.
- The query it runs counts in the database and fetches no article columns.
- It is zero when nothing is published.
Notes
- A QuerySet fetches every concrete column of the model by default. only names the columns to keep, defer names the ones to drop, and the SQL is what matters rather than which method got you there. print(qs.query) shows it.
- A trimmed row is still a real model instance, not a tuple or a dict. The dropped columns are deferred, and this is the cost to know: reading a deferred attribute silently runs one extra query, per instance, every time a fresh instance is loaded. Ten trimmed rows whose body you touch in a loop cost ten extra queries, which is worse than having fetched the column once.
- Do not read body anywhere - not in a helper, not in export_page. Touching it per row is the exact cost the trim was there to avoid.
- len(list(qs)) and bool(list(qs)) ship every matching row to Python and then throw the rows away. count() ships one number and exists() stops the database at the first match.
- select_related and prefetch_related solve a different problem and have no part in this one.
