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

Browsable Change List

easyDjango AdminModel Administration100%3 of 3 solved

Problem statement

Product is registered, so the admin has pages for it - but the change list is one column of the model's __str__, in whatever order the database felt like. Finding the discontinued items means opening rows one at a time, and nobody can see a price with tax on it without doing the arithmetic. Everything the list needs is configuration on ProductAdmin in app/admin.py, and none of it is set.

Requirements

Implement:

  • the configuration on ProductAdmin 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. Product holds name (CharField), sku (CharField, unique), price (DecimalField(max_digits=8, decimal_places=2)), status (CharField with choices draft, active and discontinued, exported as Product.DRAFT, Product.ACTIVE and Product.DISCONTINUED) and added_on (DateField). ProductAdmin is already registered for Product and its change list lives at /admin/app/product/.

1. The six columns

  • The change list renders a column for each of name, sku, price, tax_inclusive_price, status and added_on. That left-to-right order reads best, though any order is fine.

2. The computed column

  • It is named tax_inclusive_price, and it is a method on the ModelAdmin that receives the row's object. It is not a stored field, so a name in the column list does not have to be one.
  • It shows the row's price multiplied by 1.2, as a string with exactly two decimal places: 45.00 shows as 54.00, 25.50 as 30.60, and 1.11 as 1.33, so 1.332 rounds to two places rather than being truncated or shown in full.
  • Multiply by Decimal("1.2"), not by the float 1.2, so the arithmetic stays exact.
  • The column's header text is up to you.

3. Search

  • The search box looks in name and sku, and in nothing else. Searching Mouse returns only the Wireless Mouse, and searching DL-9 returns only the lamp with that code.

4. The sidebar filters

  • The sidebar offers status and added_on. A field with choices offers those choices, and Django builds its own date ranges for a date field.
  • Each narrows the list on its own, and either one still narrows while the search box is also in use, so a filter and a search compose into rows matching both.

5. The default order

  • Newest first by added_on, and within one date, by name ascending.

Notes

  • Only app/admin.py changes.
  • The columns, the search box, the sidebar filters and the default sort are each one class attribute on ProductAdmin; a - prefix sorts descending.
  • The computed column is a method on the ModelAdmin; do not add a property or a field to the model.
  • Do not add inlines, read-only fields or admin actions yet.

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 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 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.