Problem statement
The store team runs on the admin, and the admin is nothing but bare registrations: lists of __str__, no search, no filters, no order, order items on their own separate pages, no totals anywhere, and the nightly feed's bookkeeping table sitting in the menu waiting to be edited by mistake. Build the whole back office in app/admin.py - three configured change lists, an order page that edits its own items, a computed total that nobody can type into, one bulk action, and a registration list that stops where it should.
Requirements
Implement:
- the whole admin configuration in app/admin.py
The following files are already implemented and provided: app/models.py, app/views.py, app/urls.py and project/urls.py. The admin is already mounted at /admin/ and app/admin.py is empty. Five models:
| Model | Fields |
|---|---|
| Category | name, with a reverse products accessor |
| Product | name, sku (unique), price, status (draft / active / discontinued), added_on, category |
| Order | reference (unique), status (pending / shipped), placed_on, with a reverse items accessor |
| OrderItem | order, description, quantity (refuses anything below 1 at the validation layer), unit_price |
| ImportBatch | filename, started_at; written by the nightly feed, never edited by hand |
1. What staff can reach
- Category, Product and Order are each known to the admin, so each appears on the admin index at /admin/ and has a working change list at /admin/app/<model>/ answering 200, plus the add, edit and delete pages the admin builds from the model. A product round-trips through add/, <pk>/change/ and <pk>/delete/ with post=yes.
- ImportBatch is internal: its name does not appear on the index, and /admin/app/importbatch/ and /admin/app/importbatch/<pk>/change/ both answer 404.
- OrderItem is not registered on its own either. It belongs on the order page, so /admin/app/orderitem/ answers 404.
2. The category list
- Columns name and product_count, the latter a computed count of that category's products.
- The search box looks in name.
- Ordered by name ascending, so the default order does not fall back to insertion or primary key.
3. The product list
- Columns name, sku, price, tax_inclusive_price, status and added_on.
- tax_inclusive_price shows the price multiplied by Decimal("1.2") as a string with exactly two decimal places: 45.00 shows as 54.00 and 25.50 as 30.60. Multiply by the Decimal, not by the float 1.2, so the arithmetic stays exact.
- The search box looks in name and sku.
- The sidebar offers status and added_on, each narrowing the list on its own.
- Newest first by added_on, then name ascending.
4. The order list
- Columns reference, status, placed_on and total, so the computed total shows on the list as well as on the form.
- The search box looks in reference.
- The sidebar offers status and placed_on.
- Newest first by placed_on, then reference ascending, so two orders sharing the newest date come out in reference order rather than reversed by the primary-key fallback.
5. The order page
- OrderItem rows appear on the order page itself in the tabular style, with two blank rows offered, so an order with one saved item renders three item forms. Adding rows there and saving the order stores them against that order.
- A field named total sums quantity x unit_price across the order's items, as a string with exactly two decimal places: two desks at 120.00 and four chairs at 75.50 make 542.00. It appears on the form with no input to type into, so the page carries no input named total, and it follows whatever the inline just saved.
- A submitted row with a quantity of 0 brings the page back as 200 showing that row's error, with neither the row nor the order saved.
6. The bulk action
- An action named mark_shipped is offered above the order change list. It must use exactly that name, because the change list posts it by name.
- It sets status to shipped on the orders the user ticked, and on nothing else. An order nobody selected keeps its current status, and the shipped ones then show up under the sidebar's shipped filter.
Notes
- Only app/admin.py changes.
- What goes on a ModelAdmin:
| Attribute | Effect |
|---|---|
| list_display | the change list's columns, left to right (any order is fine) |
| search_fields | the fields the search box looks in |
| list_filter | the fields the sidebar offers as filters |
| ordering | the default sort, - for descending |
| inlines | related models edited on this model's page |
| readonly_fields | fields shown on the form with no input |
| actions | bulk operations offered above the change list |
- A name in list_display or readonly_fields can be a method on the ModelAdmin rather than a stored field: it receives the row's object and returns what the cell or the form should show. A method nobody lists anywhere is simply never rendered.
- An inline is a class of its own - TabularInline for a grid, StackedInline for labelled blocks - with extra blank rows. Parent and children save as one submission, and a child that fails validation re-renders the parent's page with that row's error, saving nothing.
- Registration is also the access decision: a model nobody registers has no admin pages, and its URLs return 404. Nothing else has to be switched off.
- Filters and the search box compose, and the default order still applies to whatever they leave.
- Every computed value is a ModelAdmin method; do not add properties or fields to the models.
- Column headers and the action's description text are up to you.
- The action changes only the selected orders. Reaching past the queryset to the manager is wrong even when the result looks right.
- Configure the admin and nothing else: no custom forms, no admin templates, no permission rules.
