Django admin: Format fields in list, but keep sortable?
django-admin, django-models
Solution
Read here - ModelAdmin.list_display (read a lot to get to the point ;) )
you need to add admin_order_field attribute to your function
class YourAdminCLass(admin.ModelAdmin)
[...]
def size_formatted(self, obj):
return "whatever you need"
size_formatted.admin_order_field = 'size'
Problem
I keep numeric fields like "size", "width", "height" in my database. Now I would attach units like "KiB" or "pixels" to them when showing them in the change list. This could easily be achieved by adding callables such as "size_formatted" etc to list_display. However, these are no longer sortable. Is there a way around this limitation?