StaffCoder
Django
DjangoDjango 5.2Python 3.13
18 modules · 72 challengesSign up
Modules18 modules · 72 challenges
Cut a page's query count with select_related, prefetch_related, filtered prefetches, column trimming, and database-side counts and existence checks.
Finish every challenge, or complete the star challenge, to complete the module.

Challenges

4 challenges

Select Related Detail Page

Sign up to unlock
—No data yetmedium
About this challenge

The order history page fetches the orders in one query and then fires another query per row for the customer, and another for that customer's region. Collapse the whole chain into the page's one-query budget with select_related, leaving every rendered row exactly as it was.

Read the full problem
Solved in a browser IDE and graded by tests. Sign up to solve

Prefetch Related Collections

Sign up to unlock
—No data yetmedium
About this challenge

A course listing reads each course's tags and students one course at a time, and its filtered student list re-queries on every row. Batch both collections with prefetch_related and apply the filter and the ordering through a Prefetch object.

Read the full problem
Solved in a browser IDE and graded by tests. Sign up to solve

Lean Field Loading

Sign up to unlock
—No data yetmedium
About this challenge

The exports page loads every article's full body just to list titles, and it works out its flag and its total by pulling all the rows into Python. Trim the query to the columns the page shows and let the database answer the two questions instead.

Read the full problem
Solved in a browser IDE and graded by tests. Sign up to solve

Dashboard Under Budget

STAR
Solving it completes the module
—No data yetmedium
About this challenge

The team dashboard renders correctly and costs hundreds of queries: the members arrive alone, then every row fetches its role, that role's department, its tags, its projects and its activities one at a time. Rebuild the page's data layer so the same markup renders inside a six query budget, combining a chained join, three batched collections with a filtered Prefetch, a trimmed column list and an existence check.

Read the full problem
Solved in a browser IDE and graded by tests. Sign up to solve

We use cookies that are necessary to run Staffcoder, and optional analytics cookies to see which pages get used. Analytics stays off unless you accept. Read our Cookie Policy.