Skip to main content

Python Field Formatting

You can use a Python function to dynamically format how a value is rendered in the table view.

💡Example​

Here's an example using a status_formatter on the Post model, to show visually different information about status of the Product:

catalog/sb_admin.py
def status_formatter(object_id, value):
if value:
label = "âś…"
else:
label = "❌"
return f'<span>{label}</span>'

This formatter will return a ascii icon, based on the active status of the object.

  • object_id: The primary key of the object being rendered.
  • value: The actual value of the field for that object.

Here’s how this is applied in a real admin class:​

catalog/sb_admin.py
@admin.register(Product, site=sb_admin_site)
class ProductSBAdmin(SBAdmin):
model = Product
inlines = [ProductImageInline]
sbadmin_list_display = (
"name",
"sku",
SBAdminField(name="price", title=_("Price")),
SBAdminField(name="is_active", title=_("Active"), python_formatter=status_formatter),
"manufacturer",
)
...