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

Inline Order Editing

easyDjango AdminModel Administration100%3 of 3 solved

Problem statement

An order is its line items, but the admin treats them as strangers: editing a three-line order means opening the order, then leaving it for each item's own page, and nothing anywhere adds the order up. Marking the morning's orders shipped is the same story one row at a time. All three gaps close on OrderAdmin in app/admin.py.

Requirements

Implement:

  • the inline, the computed total and the bulk action on OrderAdmin 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. Order holds reference (unique), status (pending or shipped, exported as Order.PENDING and Order.SHIPPED, defaulting to pending) and placed_on (DateField). OrderItem holds order (a foreign key to Order with related_name="items"), description, quantity (a PositiveIntegerField with a MinValueValidator(1), so the validation layer refuses anything below 1) and unit_price (DecimalField(max_digits=8, decimal_places=2)). OrderAdmin is already registered for Order with list_display = ("reference", "status", "placed_on"); keep that as it is. The order page is at /admin/app/order/<pk>/change/ and the change list at /admin/app/order/.

1. Items on the order page

  • OrderItem rows appear on the order page itself, laid out in the tabular style - a grid, one row per record - rather than on pages of their own.
  • Two blank rows are offered, so an order with one saved item renders three item forms in total.
  • Adding rows there and saving the order stores them against that order, with the typed description, quantity and unit_price. The parent and its rows save as one submission.

2. The order total

  • A field named total appears on the order page showing the sum of 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 total 542.00.
  • It is computed, so it is shown with no input to type into: the page carries no input named total.
  • The field's label is up to you.

3. The bulk action

  • An action named mark_shipped is offered above the change list. It must use exactly that name, because the change list posts it by name; its description text is up to you.
  • It sets status to shipped on the orders the user ticked, and on nothing else. An order nobody selected keeps its current status.

4. Errors land on the order page

  • A submitted item row with a quantity of 0 breaks the model's rule. The order page comes back as 200 showing that row's error, and neither the row nor the order is saved.

Notes

  • Only app/admin.py changes.
  • All three pieces are configuration on OrderAdmin: the item rows come from an inline class, the computed total must be listed as a read-only field to appear on the form at all (a method nobody lists is never rendered), and the action receives the queryset of exactly what the user ticked.
  • TabularInline lays the rows out as a grid, StackedInline stacks each one as a block of labelled fields.
  • The total is a ModelAdmin method; do not add a property or a field to the model.
  • The action changes only the selected orders. Reaching past the queryset to the manager is wrong even when the result looks right.

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 solvedeasyStore Back OfficeConfigure a complete Django admin back office: change lists with computed columns, search, filters, ordering, inline editing, read-only totals, a bulk action and correct registration scope.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.