StaffCoder
Django
DjangoDjango 5.2Python 3.13
18 modules · 72 challengesSign up
Module 5 · Django Admin

Store Back Office

easyDjango AdminModel Administration100%3 of 3 solved

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:

ModelFields
Categoryname, with a reverse products accessor
Productname, sku (unique), price, status (draft / active / discontinued), added_on, category
Orderreference (unique), status (pending / shipped), placed_on, with a reverse items accessor
OrderItemorder, description, quantity (refuses anything below 1 at the validation layer), unit_price
ImportBatchfilename, 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:
AttributeEffect
list_displaythe change list's columns, left to right (any order is fine)
search_fieldsthe fields the search box looks in
list_filterthe fields the sidebar offers as filters
orderingthe default sort, - for descending
inlinesrelated models edited on this model's page
readonly_fieldsfields shown on the form with no input
actionsbulk 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.

More in Django Admin

See the module
Registered Catalog AdminRegister the catalogue models with the Django admin so staff can create, edit and delete records through the interface.100%3 of 3 solvedeasyBrowsable Change ListConfigure a Django admin change list with columns, a computed column, search, sidebar filters and a default order.100%3 of 3 solvedeasyInline Order EditingEdit order items inline on the order page, show a read-only computed total and add a bulk admin action.100%3 of 3 solvedeasy

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.